我正在运行一些测试并需要写入文件。当我运行测试时open = (file, 'r+')
不写入文件。测试脚本如下:
class GetDetailsIP(TestGet):
def runTest(self):
self.category = ['PTZ']
try:
# This run's and return's a value
result = self.client.service.Get(self.category)
mylogfile = open("test.txt", "r+")
print >>mylogfile, result
result = ("".join(mylogfile.readlines()[2]))
result = str(result.split(':')[1].lstrip("//").split("/")[0])
mylogfile.close()
except suds.WebFault, e:
assert False
except Exception, e:
pass
finally:
if 'result' in locals():
self.assertEquals(result, self.camera_ip)
else:
assert False
当此测试运行时,文本文件中没有输入任何值,并且在变量结果中返回一个值。
我也尝试过mylogfile.write(result)
。如果该文件不存在,则声明该文件不存在并且不会创建该文件。
这可能是不允许 python 创建文件的权限问题吗?我已确保对该文件的所有其他读取均已关闭,因此该文件不应被锁定。
谁能提出任何建议为什么会发生这种情况?
Thanks
写入后,光标位于文件末尾。如果您想阅读文本,则必须移至开头:
>>> mylogfile = open("test10.txt", "w+")
>>> print >> mylogfile, 'hola'
>>> mylogfile.flush() #just in case
>>> print mylogfile.read()
#nothing because I'am at the end of the file
>>> mylogfile.seek(0)
>>> print mylogfile.read()
hola
或者,如果您在阅读之前关闭文件,它也可以工作(但对于您的情况来说,这可能不是更有效的方法)。
>>> mylogfile = open("test.txt", "w")
>>> print >> mylogfile, 'hola'
>>> mylogfile.close()
>>> mylogfile = open("test.txt", "r")
>>> print mylogfile.read()
hola
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)