python - 在 Windows 中的 Python 文件上混合 read() 和 write()

标签 python windows file-io

在用 r+(或 r+ b) Windows 中的权限不会更新文件。

假设当前目录下有一个文件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(),它表明位置已经更新(在 write() 之前是 4 8 之后),但文件没有变化。

但是,如果我在 write() 行之前放置一个明确的 fd.seek(4),那么一切都会如我所料。

有人知道在 Windows 下出现这种行为的原因吗?

作为引用,我在带有 NTFS 分区的 Windows 7 上使用 Python 2.7.3。

编辑

为了回应评论,我尝试了 r+brb+ - 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 pagefopen 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/14279658/

相关文章:

python - 具有内置输入函数的类方法(Python)

python - 使用 Flask 正确重载 json 编码和解码

Python 读写 tty

c++ - 验证 Win32 窗口句柄

c++ - CArray 的析构函数大约需要 30 秒才能运行

Java,从 ArrayList 中删除对象重写为文本文件

python - Django + SQLAlchemy + RESTful API(美味?)

windows - windbg预览是否可以编辑内存数据或注册然后保存状态与时间旅行调试?

powershell - powershell out-file如何消除每行上的多余空间

matlab - matlab中的load命令加载空白文件