在浏览了整个互联网之后,我来到了这里.
假设我已经创建了一个文本文件:
Hello World
好吧,我想d从这个文本文件中删除最后一个字符(在本例中).
所以现在文本文件应该如下所示: Hello Worl
但我不知道该怎么做.
我想要的或多或少都是我硬盘上文本文件的单一退格功能.
这需要在Linux上工作,就像我正在使用的那样.
Mar*_*ers 59
用于file.seek()从末尾寻找1个位置,然后用于file.truncate()删除文件的其余部分:
with open(filename, 'rb+') as filehandle:
filehandle.seek(-1, os.SEEK_END)
filehandle.truncate()
Run Code Online (Sandbox Code Playgroud)
接受Martijn的答案很简单,也很有效,但是不能解释具有以下内容的文本文件:
vim或gedit)中的默认字符)如果文本文件包含非英语字符,那么到目前为止提供的答案均无效。
下面是一个示例,它解决了两个问题,也允许从文件末尾删除多个字符:
import os
def truncate_utf8_chars(filename, count, ignore_newlines=True):
"""
Truncates last `count` characters of a text file encoded in UTF-8.
:param filename: The path to the text file to read
:param count: Number of UTF-8 characters to remove from the end of the file
:param ignore_newlines: Set to true, if the newline character at the end of the file should be ignored
"""
with open(filename, 'rb+') as f:
last_char = None
size = os.fstat(f.fileno()).st_size
offset = 1
chars = 0
while offset <= size:
f.seek(-offset, os.SEEK_END)
b = ord(f.read(1))
if ignore_newlines:
if b == 0x0D or b == 0x0A:
offset += 1
continue
if b & 0b10000000 == 0 or b & 0b11000000 == 0b11000000:
# This is the first byte of a UTF8 character
chars += 1
if chars == count:
# When `count` number of characters have been found, move current position back
# with one byte (to include the byte just checked) and truncate the file
f.seek(-1, os.SEEK_CUR)
f.truncate()
return
offset += 1
Run Code Online (Sandbox Code Playgroud)
怎么运行的:
样本文本文件- bg.txt:
??????? ????
Run Code Online (Sandbox Code Playgroud)
如何使用:
filename = 'bg.txt'
print('Before truncate:', open(filename).read())
truncate_utf8_chars(filename, 1)
print('After truncate:', open(filename).read())
Run Code Online (Sandbox Code Playgroud)
输出:
Before truncate: ??????? ????
After truncate: ??????? ???
Run Code Online (Sandbox Code Playgroud)
这适用于UTF-8和ASCII编码文件。
with open(urfile, 'rb+') as f:
f.seek(0,2) # end of file
size=f.tell() # the size...
f.truncate(size-1) # truncate at that size - how ever many characters
Run Code Online (Sandbox Code Playgroud)
一定要在 Windows 上使用二进制模式,因为 Unix 文件行结尾 many 返回非法或不正确的字符数。
如果你没有以二进制模式读取文件,你只有'w'权限,我可以建议如下.
f.seek(f.tell() - 1, os.SEEK_SET)
f.write('')
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,f.seek()只接受f.tell()您没有"b"访问权限的b/c.然后你可以将光标设置为最后一个元素的开头.然后,您可以用空字符串删除最后一个元素.