QTP操作txt文档

QTP可以在txt文件(文本文件中读取数据)

首先创造一个文档对象 set fso = createObject("scripting.filesystemobject")

然后用此对象打开目标文档 Set txt = fso.OpenTextFile( "C:\Documents and Settings\Administrator\桌面\test.txt",8,true)

这里说一说OpenTextFile方法,根据QTP的帮助文档中记载

根据以上帮助文档记录,我们现在要实现的是读取一个txt文档,OpenTextFile方法里的iomode模式采用只读模式(ForReading|1)代码如下:

Option explicit
Dim fso 
set fso = createObject("scripting.filesystemobject")
Dim txt
Set txt = fso.OpenTextFile( "C:\Documents and Settings\Administrator\桌面\test.txt",1,true)
'Skips the next line when reading a TextStream file.忽略掉文档中的第一行
'txt.SkipLine
Dim content
While not txt.AtEndOfStream
	'读取txt文档中的一行
	content =   txt.ReadLine
	print(content)
	print("------------------------")
Wend
txt.Close
Set txt = nothing
Set fso = nothing

好了,上述操作即实现了对txt文档的读取操作。

接着,我们实现向txt文档写入内容的操作,iomode采用写入的模式ForWriting|2和ForAppending|8都可以。实现代码如下:

Option explicit
Dim fso 
set fso = createObject("scripting.filesystemobject")
Dim txt
Set txt = fso.OpenTextFile( "C:\Documents and Settings\Administrator\桌面\test.txt",8,true)
txt.WriteLine("This is a new line")
txt.Close
Set txt = nothing
Set fso = nothing

 

 

转载于:https://www.cnblogs.com/huang1990/p/3686153.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值