import mmap
# Create test file for demonstration (about 50MB)
#
# The quick brown fox jumped over 1 lazy dogs
# The quick brown fox jumped over 2 lazy dogs
# ...
# The quick brown fox jumped over 1,000,000 lazy dogs
with open('test.txt', 'w') as f:
for i in range(1, 1_000_001):
print(f'The quick brown fox jumped over {i:,} lazy dogs', file=f)
# Create memory-mapped file, read first line, shift file memory
# starting from offset of the 2nd line back to the beginning of the file.
# This removes the first line.
with open('test.txt', 'r+b') as f:
with mmap.mmap(f.fileno(), 0) as mm:
size = mm.size()
line = mm.readline()
linelen = len(line)
mm.move(0, linelen, size - linelen)
mm.flush()
# Truncate the file to the shorter length.
f.truncate(size - linelen)
# Read the first line of the new file.
with open('test.txt') as f:
print(f.readline())
import fileinput
with fileinput.input(files=('text.txt'), inplace=True) as f:
for line_number, line in enumerate(f):
if line_number == 0:
continue
print(line, end='')
2条答案
按热度按时间3bygqnnd1#
您可以将文件的内存Map到出现在内存中的文件的内容,然后将内存从第2行开始移动到文件的开头。然后将文件截断为新文件长度。
对于70GB的文件来说,这可能不会太快。它仍然需要将文件更改刷新回磁盘。这只是文件的工作方式,但它不需要额外的70GB磁盘空间,如编写新文件和删除旧文件的常规过程。
输出:
kqqjbcuj2#
不幸的是,它是不可能立即删除它,但uou可以尝试这个代码。这将基本上重写同一文件中的内容,除了第一行:
inplace=True
参数告诉Python修改文件,而不是创建一个新文件。