Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python文件I/o中,truncate是什么意思?_Python 3.x - Fatal编程技术网

Python 3.x 在python文件I/o中,truncate是什么意思?

Python 3.x 在python文件I/o中,truncate是什么意思?,python-3.x,Python 3.x,我偶然发现了这篇文章,它告诉我w+可以截断一个文件,而r+文件模式不能 我很难理解truncate的含义以及file.truncate的作用。简而言之,truncate是指从当前位置到文件末尾从文件中删除文本 假设一个文件有5行: file.readline() # reads the first line file.truncate() # removes the lines 2 through 5. file.readline() # returns '' since lines have

我偶然发现了这篇文章,它告诉我w+可以截断一个文件,而r+文件模式不能

我很难理解truncate的含义以及file.truncate的作用。

简而言之,truncate是指从当前位置到文件末尾从文件中删除文本

假设一个文件有5行:

file.readline() # reads the first line
file.truncate() # removes the lines 2 through 5.
file.readline() # returns '' since lines have been deleted
file.seek(0) # moves the cursor to the start of the file
file.readline() # reads the first line again
简而言之,truncate是指从文件的当前位置直到文件末尾删除文本

假设一个文件有5行:

file.readline() # reads the first line
file.truncate() # removes the lines 2 through 5.
file.readline() # returns '' since lines have been deleted
file.seek(0) # moves the cursor to the start of the file
file.readline() # reads the first line again

如果这是一个自我回答的问题-这甚至不是一个正确的答案…@JonClements欢迎你提供一个更好的答案,并否决任何你不喜欢的问题如果这是一个自我回答的问题-这甚至不是一个正确的答案…@JonClements欢迎你提供一个更好的答案并否决任何你不喜欢重复答案的复制品都比你自己的答案更具启发性——看看他们是否为你澄清了问题并考虑关闭这个问题。可能的重复答案对你来说是很有启发性的。问题