python读取和写入文件_初学者Python:读取和写入同一文件

Started Python a week ago and I have some questions to ask about reading and writing to the same files. I've gone through some tutorials online but I am still confused about it. I can understand simple read and write files.

openFile = open("filepath", "r")

readFile = openFile.read()

print readFile

openFile = open("filepath", "a")

appendFile = openFile.write("\nTest 123")

openFile.close()

But, if I try the following I get a bunch of unknown text in the text file I am writing to. Can anyone explain why I am getting such errors and why I cannot use the same openFile object the way shown below.

# I get an error when I use the codes below:

openFile = open("filepath", "r+")

writeFile = openFile.write("Test abc")

readFile = openFile.read()

print readFile

openFile.close()

I will try to clarify my problems. In the example above, openFile is the object used to open file. I have no problems if I want write to it the first time. If I want to use the same openFile to read files or append something to it. It doesn't happen or an error is given. I have to declare the same/different open file object before I can perform another read/write action to the same file.

#I have no problems if I do this:

openFile = open("filepath", "r+")

writeFile = openFile.write("Test abc")

openFile2 = open("filepath", "r+")

readFile = openFile2.read()

print readFile

openFile.close()

I will be grateful if anyone can tell me what I did wrong here or is it just a Pythong thing. I am using Python 2.7. Thanks!

解决方案

Updated Response:

This seems like a bug specific to Windows - http://bugs.python.org/issue1521491.

the effect of mixing reads with writes on a file open for update is

entirely undefined unless a file-positioning operation occurs between

them (for example, a seek()). I can't guess what

you expect to happen, but seems most likely that what you

intend could be obtained reliably by inserting

fp.seek(fp.tell())

between read() and your write().

My original response demonstrates how reading/writing on the same file opened for appending works. It is apparently not true if you are using Windows.

Original Response:

In 'r+' mode, using write method will write the string object to the file based on where the pointer is. In your case, it will append the string "Test abc" to the start of the file. See an example below:

>>> f=open("a","r+")

>>> f.read()

'Test abc\nfasdfafasdfa\nsdfgsd\n'

>>> f.write("foooooooooooooo")

>>> f.close()

>>> f=open("a","r+")

>>> f.read()

'Test abc\nfasdfafasdfa\nsdfgsd\nfoooooooooooooo'

The string "foooooooooooooo" got appended at the end of the file since the pointer was already at the end of the file.

Are you on a system that differentiates between binary and text files? You might want to use 'rb+' as a mode in that case.

Append 'b' to the mode to open the file in binary mode, on systems

that differentiate between binary and text files; on systems that

don’t have this distinction, adding the 'b' has no effect.

http://docs.python.org/2/library/functions.html#open

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值