初学者Python:读取和写入相同的文件 [英] Beginner Python: Reading and writing to the same file

查看:142
本文介绍了初学者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()

但是,如果我尝试以下操作,则会在要写入的文本文件中收到一堆未知文本.谁能解释为什么我会收到这样的错误,以及为什么我不能以如下所示的方式使用相同的openFile对象.

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

我将尝试澄清我的问题.在上面的示例中, openFile 是用于打开文件的对象.如果我想第一次写它,我没有问题.如果我想使用相同的 openFile 来读取文件或向其中添加内容.它不会发生或给出错误.我必须声明相同/不同的打开文件对象,然后才能对同一文件执行另一次读/写操作.

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

如果有人能告诉我我在这里做错了还是仅仅是Pythong的事情,我将不胜感激.我正在使用Python 2.7.谢谢!

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!

推荐答案

更新后的响应:

这似乎是Windows特有的错误- http://bugs.python.org/issue1521491

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

http://上解释的替代方法的报价mail.python.org/pipermail/python-bugs-list/2005-August/029886.html

将打开的文件中的读取和写入混合在一起的效果是 除非文件之间存在文件定位操作,否则完全未定义 他们(例如,seek()).我猜不到 您预期会发生,但似乎最有可能 插入即可可靠地获得意图

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

fp.seek(fp.tell())

在read()和您的write()之间.

between read() and your write().

我的原始回复演示了如何在打开的用于追加的同一文件上进行读取/写入.如果您使用Windows,显然是不正确的.

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.

原始回复:

在"r +"模式下,使用write方法将根据指针所在的位置将字符串对象写入文件.在您的情况下,它将在字符串后附加字符串"Test abc"到文件的开头.请参见下面的示例:

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'

字符串"foooooooooooooo"因为指针已经在文件末尾,所以被附加在文件末尾.

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

您是否在区分二进制文件和文本文件的系统上?在这种情况下,您可能希望使用"rb +"作为模式.

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

在系统上将'b'附加到以二进制模式打开文件的模式 区分二进制文件和文本文件;在系统上 没有这种区别,添加"b"无效. http://docs.python.org/2/library/functions.html#open

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

这篇关于初学者Python:读取和写入相同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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