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

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

问题描述

一周前开始使用 Python,我有一些关于读取和写入相同文件的问题要问.我已经在网上浏览了一些教程,但我仍然对此感到困惑.能看懂简单的文件读写.

openFile = open("文件路径", "r")readFile = openFile.read()打印读取文件openFile = open("文件路径", "a")appendFile = openFile.write("
Test 123")openFile.close()

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

# 使用以下代码时出现错误:openFile = open("文件路径", "r+")writeFile = openFile.write("测试 abc")readFile = openFile.read()打印读取文件openFile.close()

我会尽力澄清我的问题.在上面的例子中,openFile 是用来打开文件的对象.如果我想第一次写它,我没有问题.如果我想使用相同的 openFile 来读取文件或向其附加某些内容.它不会发生或给出错误.在对同一个文件执行另一个读/写操作之前,我必须声明相同/不同的打开文件对象.

#如果我这样做没有问题:openFile = open("文件路径", "r+")writeFile = openFile.write("测试 abc")openFile2 = open("文件路径", "r+")readFile = openFile2.read()打印读取文件openFile.close()

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

解决方案

更新回复:

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

引用 http://mail.python.org/pipermail/python-bugs-list/2005-August/029886.html

<块引用>

混合读和写对打开更新的文件的影响是完全未定义,除非文件定位操作发生在它们(例如,seek()).我猜不出是什么你期望发生,但似乎最有可能你可以通过插入可靠地获得意图

fp.seek(fp.tell())

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

我的原始回复演示了对打开以进行追加的同一文件的读/写是如何工作的.如果您使用的是 Windows,这显然不是真的.

原始回复:

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

<预><代码>>>>f=open(a",r+")>>>f.read()'测试 abc fasdfafasdfa sdfgsd '>>>f.write(foooooooooooooo")>>>f.close()>>>f=open(a",r+")>>>f.read()'测试 abc fasdfafasdfa sdfgsd foooooooooooooo'

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

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

<块引用>

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

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("
Test 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.

Quoting from the workaround explained at http://mail.python.org/pipermail/python-bugs-list/2005-August/029886.html

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
fasdfafasdfa
sdfgsd
'
>>> f.write("foooooooooooooo")
>>> f.close()
>>> f=open("a","r+")
>>> f.read()
'Test abc
fasdfafasdfa
sdfgsd
foooooooooooooo'

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

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

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