Python 3:删除文件中最后一个字符的惯用方法
Python 3: Idiomatic way to delete last char in file
有没有惯用的方法来做到这一点?我刚刚从 Python 2 升级到 Python 3,我正在尝试移植我的脚本,但我不得不说我没有留下深刻印象。据我所知,我的代码开始运行了
由此
# Not allowed by Python 3 anymore without being in binary mode.
card_names_file.seek(-1, os.SEEK_END)
if card_names_file.read() == ',':
card_names_file.truncate()
至此
# Go to end of file just to get position index. lawl.
card_names_file.seek(0, os.SEEK_END)
# Create a temporary just to store said index. More lawl.
eof = card_names_file.tell()
# Index one from the back. ugh. w/e, that's fine.
card_names_file.seek(eof - 1, os.SEEK_SET)
# Oh wait, .read() will advance my pointer. Oh hey Python 3 doesn't let me
# use .peek() either. Fantastic. I'll have to read this...
if card_names_file.read() == ',':
# Then go back to where I was by indexing from front AGAIN
card_names_file.seek(eof - 1, os.SEEK_SET)
# Then remove last character.
card_names_file.truncate()
这是我见过的最愚蠢的代码,到目前为止,我已经花了 2 个半小时试图从文件后面删除一个字符,这看起来像是一个 hack。
另一种方法是我有这样的代码
# open file
with open(file, a+)
# do stuff
# open same file
with open(file, w+b)
# do different stuff
但我实际上也无法让它工作。
底层缓冲区确实有您正在寻找的 peek()
方法,因此:
f = open('FILE', 'a+')
f.seek(f.seek(0, os.SEEK_END) - 1)
# or with the same effect you can also:
os.lseek(f.fileno(), -1, os.SEEK_END)
# Actually in append mode we could just seek by -1 from where we are
# (cursor at the end after opening)
f.tell() # just to see...
f.buffer.peek(1)
f.tell() # ...still where we were
或者,您也可以使用 os.pread()
。例如:
os.pread(f.fileno(), 1, os.fstat(f.fileno()).st_size - 1)
依赖于更高层次的抽象访问文件并不是很惯用,但我会invoke:"Although practicality beats purity."
有没有惯用的方法来做到这一点?我刚刚从 Python 2 升级到 Python 3,我正在尝试移植我的脚本,但我不得不说我没有留下深刻印象。据我所知,我的代码开始运行了
由此
# Not allowed by Python 3 anymore without being in binary mode.
card_names_file.seek(-1, os.SEEK_END)
if card_names_file.read() == ',':
card_names_file.truncate()
至此
# Go to end of file just to get position index. lawl.
card_names_file.seek(0, os.SEEK_END)
# Create a temporary just to store said index. More lawl.
eof = card_names_file.tell()
# Index one from the back. ugh. w/e, that's fine.
card_names_file.seek(eof - 1, os.SEEK_SET)
# Oh wait, .read() will advance my pointer. Oh hey Python 3 doesn't let me
# use .peek() either. Fantastic. I'll have to read this...
if card_names_file.read() == ',':
# Then go back to where I was by indexing from front AGAIN
card_names_file.seek(eof - 1, os.SEEK_SET)
# Then remove last character.
card_names_file.truncate()
这是我见过的最愚蠢的代码,到目前为止,我已经花了 2 个半小时试图从文件后面删除一个字符,这看起来像是一个 hack。
另一种方法是我有这样的代码
# open file
with open(file, a+)
# do stuff
# open same file
with open(file, w+b)
# do different stuff
但我实际上也无法让它工作。
底层缓冲区确实有您正在寻找的 peek()
方法,因此:
f = open('FILE', 'a+')
f.seek(f.seek(0, os.SEEK_END) - 1)
# or with the same effect you can also:
os.lseek(f.fileno(), -1, os.SEEK_END)
# Actually in append mode we could just seek by -1 from where we are
# (cursor at the end after opening)
f.tell() # just to see...
f.buffer.peek(1)
f.tell() # ...still where we were
或者,您也可以使用 os.pread()
。例如:
os.pread(f.fileno(), 1, os.fstat(f.fileno()).st_size - 1)
依赖于更高层次的抽象访问文件并不是很惯用,但我会invoke:"Although practicality beats purity."