简体   繁体   English

Python的读写操作会将\\ x00添加到文件中

[英]Python's read and write add \x00 to the file

I have come across a weird problem when working with files in python. 在python中处理文件时遇到了一个奇怪的问题。 Let's say I have a text file and a simple piece of code that reads the contents of the file and then rewrites it with unaltered contents. 假设我有一个文本文件和一段简单的代码来读取文件的内容,然后用未改变的内容重写它。

File.txt FILE.TXT

This is a test file 这是一个测试文件

Python code Python代码

f=open(File.txt,'r+')
data=f.read()
f.truncate(0)
f.write(data)
f.close()

After running the above code File.txt seems to be the same. 运行上面的代码后, File.txt似乎是一样的。 However, when I opened it in a hex editor I was surprised to see lots of \\x00 (NULL) bytes before the actual contents of the text file, that wasn't there before. 但是,当我在十六进制编辑器中打开它时,我惊讶地发现在文本文件的实际内容之前有很多\\x00 (NULL)字节,之前没有

Can anyone please explain? 有人可以解释一下吗?

Suppose your file has 20 bytes in it. 假设您的文件中包含20个字节。 So f.read() reads 20 bytes. 所以f.read()读取20个字节。 Now you truncate the file to 0 bytes. 现在您将文件截断为0字节。 But your position-in-file pointer is still at 20. Why wouldn't it be? 但是你的文件位置指针仍然是20.为什么不是? You haven't moved it. 你没有动过它。 So when you write, you begin writing at the 21st byte. 所以当你写作时,你开始写第21个字节。 Your OS fills in the 20 missing bytes with zeroes. 您的操作系统用零填充了20个丢失的字节。

To avoid this, f.seek(0) before writing again. 为了避免这种情况, f.seek(0)在再次写入之前f.seek(0)

f.truncate(0) sets all bytes of the file to \\x00 . f.truncate(0)将文件的所有字节设置为\\x00 However, it does not change the file pointer - you're still at the position after the call to read . 但是,它不会更改文件指针 - 您仍然在调用read之后的位置。 Therefore, if you write anything, the operating system will extend the file to the new length (the original length + len(data) ). 因此,如果您编写任何内容,操作系统会将文件扩展到新的长度(原始长度+ len(data) )。

To avoid that, call seek : 为了避免这种情况,打电话seek

with open('File.txt', 'r+') as f:
  data=f.read()
  f.seek(0)
  f.truncate(0)
  f.write(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM