似乎在 Windows 中使用 write()
(或 read()
)权限打开的文件上的 r+
后紧跟 r+b
不会更新文件。
假设当前目录下有一个文件testfile.txt
,内容如下:
This is a test file.
我执行以下代码:
with open("testfile.txt", "r+b") as fd:
print fd.read(4)
fd.write("----")
我希望代码打印
This
并将文件内容更新为:This----a test file.
这至少在 Linux 上运行良好。但是,当我在 Windows 上运行它时,消息会正确显示,但文件没有改变 - 就像
write()
被忽略一样。如果我在文件句柄上调用 tell()
,它会显示位置已更新(在 4
之前是 write()
,之后是 8
),但文件没有更改。但是,如果我在
fd.seek(4)
行之前放置一个明确的 write()
,那么一切都会按我的预期工作。有人知道 Windows 下这种行为的原因吗?
作为引用,我在带有 NTFS 分区的 Windows 7 上使用 Python 2.7.3。
编辑
针对评论,我尝试了
r+b
和 rb+
- official Python docs 似乎暗示前者是规范的。我在不同的地方调用
fd.flush()
,并在 read()
和 write()
之间放置一个,如下所示:with open("testfile.txt", "r+b") as fd:
print fd.read(4)
fd.flush()
fd.write("----")
...产生以下有趣的错误:
IOError: [Errno 0] Error
编辑 2
间接地,添加
flush()
有所帮助,因为它使我想到 this post 描述了类似的问题。如果其中一位评论者是正确的,则它是底层 Windows C 库中的一个错误。
最佳答案
Python 的文件操作应该遵循 libc
约定,因为它在内部使用 C 文件 IO 函数实现。
引用 fopen man page 或 fopen page in cplusplus
For files open for appending (those which include a "+" sign), on which both input and output operations are allowed, the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a writing operation followed by a reading operation or a reading operation which did not reach the end-of-file followed by a writing operation.
所以总结一下,如果您需要在写入后读取文件,则需要对缓冲区进行
fflush
并且读取后的写入操作应在 fseek
之前,如 fd.seek(0, os.SEEK_CUR)
所以只需将您的代码片段更改为
with open("test1.txt", "r+b") as fd:
print fd.read(4)
fd.seek(0, os.SEEK_CUR)
fd.write("----")
该行为与类似的 C 程序的行为方式一致
#include <cstdio>
int main()
{
char buffer[5] = {0};
FILE *fp = fopen("D:\\Temp\\test1.txt","rb+");
fread(buffer, sizeof(char), 4, fp);
printf("%s\n", buffer);
/*without fseek, file would not be updated*/
fseek(fp, 0, SEEK_CUR);
fwrite("----",sizeof(char), 4, fp);
fclose(fp);
return 0;
}
关于python - 在 Windows 中的 Python 文件上混合使用 read() 和 write(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63957357/