无法写入文本文件 [英] Unable to write to a text file

查看:139
本文介绍了无法写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些测试,需要写入一个文件。当我运行测试时, open =(file,'r +')不写入文件。测试脚本如下:

 类GetDetailsIP(TestGet):

run runTest(self):

self.category = ['PTZ']

try:
#这个运行并返回一个值
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()
除了suds.WebFault,e:
断言False
,Exception:e:
传递
finally:
():
self.assertEquals(result,self.camera_ip)
else:
assert False

当这个测试运行的时候,没有值ha已经被输入到文本文件中,并且在变量结果中返回一个值。

我也试过 mylogfile.write(result) code>。如果该文件不存在是声明的文件不存在,并不会创建一个。

这可能是一个权限问题,其中python不允许创建一个文件?我已经确保所有其他阅读这个文件已关闭,所以我的文件不应该被锁定。

任何人都可以提供任何建议,为什么发生这种情况?



谢谢

解决方案

写完后,光标位于文件末尾。如果你想读的文本,你必须移动到开始:

 >>> mylogfile = open(test10.txt,w +)
>>>打印>> mylogfile,'hola'
>>> mylogfile.flush()#如果是
>>>打印mylogfile.read()
#因为我在文件的末尾
>>> mylogfile.seek(0)
>>>打印mylogfile.read()
hola

另外,如果关闭文件在阅读之前(但也许这不是您的情况下更有效的方法)。

 >>> mylogfile = open(test.txt,w)
>>>打印>> mylogfile,'hola'
>>> mylogfile.close()
>>> mylogfile = open(test.txt,r)
>>>打印mylogfile.read()
hola


I am running some tests and need to write to a file. When I run the test's the open = (file, 'r+') does not write to the file. The test script is below:

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

When this test run's, no value has been entered into the text file and a value is returned in the variable result.

I havw also tried mylogfile.write(result). If the file does not exist is claim's the file does not exist and doesn't create one.

Could this be a permission problem where python is not allowed to create a file? I have made sure that all other read's to this file are closed so I the file should not be locked.

Can anyone offer any suggestion why this is happening?

Thanks

解决方案

After writing, your cursor is at the end of the file. If you want to read the text you have to move to the beginning:

>>> 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

Alternatively, it also works if you close your file before reading (but maybe this is not the more efficient method for your case).

>>> mylogfile = open("test.txt", "w")
>>> print >> mylogfile, 'hola' 
>>> mylogfile.close()
>>> mylogfile = open("test.txt", "r")
>>> print mylogfile.read()
hola

这篇关于无法写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆