Python 3.x 在python文件I/o中,truncate是什么意思?
我偶然发现了这篇文章,它告诉我w+可以截断一个文件,而r+文件模式不能 我很难理解truncate的含义以及file.truncate的作用。简而言之,truncate是指从当前位置到文件末尾从文件中删除文本 假设一个文件有5行: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
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欢迎你提供一个更好的答案并否决任何你不喜欢重复答案的复制品都比你自己的答案更具启发性——看看他们是否为你澄清了问题并考虑关闭这个问题。可能的重复答案对你来说是很有启发性的。问题