就地编辑文件是一项充满陷阱的任务(很像在迭代时修改可迭代对象),通常不值得这么麻烦。在大多数情况下,写入临时文件(或工作内存,取决于您拥有更多的存储空间或 RAM)然后删除源文件并用临时文件替换源文件将与尝试执行相同的性能同样的事情。
但是,如果您坚持,这里有一个通用的解决方案:
import os
def remove_line(path, comp):
with open(path, "r+b") as f: # open the file in rw mode
mod_lines = 0 # hold the overwrite offset
while True:
last_pos = f.tell() # keep the last line position
line = f.readline() # read the next line
if not line: # EOF
break
if mod_lines: # we've already encountered what we search for
f.seek(last_pos - mod_lines) # move back to the beginning of the gap
f.write(line) # fill the gap with the current line
f.seek(mod_lines, os.SEEK_CUR) # move forward til the next line start
elif comp(line): # search for our data
mod_lines = len(line) # store the offset when found to create a gap
f.seek(last_pos - mod_lines) # seek back the extra removed characters
f.truncate() # truncate the rest
这将仅删除与提供的比较函数匹配的行,然后遍历文件的其余部分,将数据移到“已删除”行上。您也不需要将文件的其余部分加载到工作内存中。要测试它,test.csv 包含:
fname,lname,age,sex
约翰,能源部,28,米
莎拉,史密斯,27,f
泽维尔,摩尔,19,米
你可以这样运行它:
remove_line("test.csv", lambda x: x.startswith(b"Sarah"))
您将获得 test.csv 并在原地删除 Sarah 行:
fname,lname,age,sex
约翰,能源部,28,米
泽维尔,摩尔,19,米
请记住,当文件以二进制模式打开时,我们传递了一个 bytes 比较函数,以便在截断/覆盖时保持一致的换行符。
更新:我对这里介绍的各种技术的实际性能很感兴趣,但我昨天没有时间测试它们,所以稍微延迟了我创建了一个基准这可能会有所启发。如果您只对结果感兴趣,请一直向下滚动。首先,我将解释我的基准测试是什么以及我是如何设置测试的。我还将提供所有脚本,以便您可以在您的系统上运行相同的基准测试。
至于什么,我已经测试了这个和其他答案中提到的所有技术,即使用临时文件(temp_file_* 函数)和使用就地编辑(in_place_*)函数进行行替换。我在流(逐行读取,*_stream 函数)和内存(在工作内存中读取文件的其余部分,*_wm 函数)模式中都设置了这两种模式。我还使用mmap 模块(in_place_mmap 函数)添加了就地行删除技术。包含所有功能以及需要通过 CLI 控制的少量逻辑的基准脚本如下:
#!/usr/bin/env python
import mmap
import os
import shutil
import sys
import time
def get_temporary_path(path): # use tempfile facilities in production
folder, filename = os.path.split(path)
return os.path.join(folder, "~$" + filename)
def temp_file_wm(path, comp):
path_out = get_temporary_path(path)
with open(path, "rb") as f_in, open(path_out, "wb") as f_out:
while True:
line = f_in.readline()
if not line:
break
if comp(line):
f_out.write(f_in.read())
break
else:
f_out.write(line)
f_out.flush()
os.fsync(f_out.fileno())
shutil.move(path_out, path)
def temp_file_stream(path, comp):
path_out = get_temporary_path(path)
not_found = True # a flag to stop comparison after the first match, for fairness
with open(path, "rb") as f_in, open(path_out, "wb") as f_out:
while True:
line = f_in.readline()
if not line:
break
if not_found and comp(line):
continue
f_out.write(line)
f_out.flush()
os.fsync(f_out.fileno())
shutil.move(path_out, path)
def in_place_wm(path, comp):
with open(path, "r+b") as f:
while True:
last_pos = f.tell()
line = f.readline()
if not line:
break
if comp(line):
rest = f.read()
f.seek(last_pos)
f.write(rest)
break
f.truncate()
f.flush()
os.fsync(f.fileno())
def in_place_stream(path, comp):
with open(path, "r+b") as f:
mod_lines = 0
while True:
last_pos = f.tell()
line = f.readline()
if not line:
break
if mod_lines:
f.seek(last_pos - mod_lines)
f.write(line)
f.seek(mod_lines, os.SEEK_CUR)
elif comp(line):
mod_lines = len(line)
f.seek(last_pos - mod_lines)
f.truncate()
f.flush()
os.fsync(f.fileno())
def in_place_mmap(path, comp):
with open(path, "r+b") as f:
stream = mmap.mmap(f.fileno(), 0)
total_size = len(stream)
while True:
last_pos = stream.tell()
line = stream.readline()
if not line:
break
if comp(line):
current_pos = stream.tell()
stream.move(last_pos, current_pos, total_size - current_pos)
total_size -= len(line)
break
stream.flush()
stream.close()
f.truncate(total_size)
f.flush()
os.fsync(f.fileno())
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: {} target_file.ext <search_string> [function_name]".format(__file__))
exit(1)
target_file = sys.argv[1]
search_func = globals().get(sys.argv[3] if len(sys.argv) > 3 else None, in_place_wm)
start_time = time.time()
search_func(target_file, lambda x: x.startswith(sys.argv[2].encode("utf-8")))
# some info for the test runner...
print("python_version: " + sys.version.split()[0])
print("python_time: {:.2f}".format(time.time() - start_time))
下一步是构建一个测试器,在尽可能隔离的环境中运行这些函数,尝试为每个函数获得一个公平的基准。我的测试结构如下:
- 三个样本数据 CSV 生成为 1Mx10 的随机数矩阵(约 200MB 文件),并在它们的开头、中间和结尾分别放置一条可识别的线,从而为三个极端场景生成测试用例。
- 在每次测试之前,主样本数据文件都被复制为临时文件(因为删除行具有破坏性)。
- 采用各种文件同步和缓存清除方法,以确保在每次测试开始前清理缓冲区。
- 测试是使用最高优先级 (
chrt -f 99) 到 /usr/bin/time 运行的基准测试,因为不能真正信任 Python 在此类场景中准确衡量其性能。
- 每个测试至少运行 3 次,以消除不可预测的波动。
- 测试也在 Python 2.7 和 Python 3.6 (CPython) 中运行,以查看版本之间是否存在性能一致性。
- 收集所有基准数据并将其保存为 CSV,以供将来分析。
不幸的是,我手头没有可以完全隔离运行测试的系统,因此我的数字是通过在管理程序中运行它获得的。这意味着 I/O 性能可能非常不平衡,但它应该同样影响仍然提供可比数据的所有测试。无论哪种方式,都欢迎您在自己的系统上运行此测试以获得您可以关联的结果。
我已将执行上述场景的测试脚本设置为:
#!/usr/bin/env python
import collections
import os
import random
import shutil
import subprocess
import sys
import time
try:
range = xrange # cover Python 2.x
except NameError:
pass
try:
DEV_NULL = subprocess.DEVNULL
except AttributeError:
DEV_NULL = open(os.devnull, "wb") # cover Python 2.x
SAMPLE_ROWS = 10**6 # 1M lines
TEST_LOOPS = 3
CALL_SCRIPT = os.path.join(os.getcwd(), "remove_line.py") # the above script
def get_temporary_path(path):
folder, filename = os.path.split(path)
return os.path.join(folder, "~$" + filename)
def generate_samples(path, data="LINE", rows=10**6, columns=10): # 1Mx10 default matrix
sample_beginning = os.path.join(path, "sample_beg.csv")
sample_middle = os.path.join(path, "sample_mid.csv")
sample_end = os.path.join(path, "sample_end.csv")
separator = os.linesep
middle_row = rows // 2
with open(sample_beginning, "w") as f_b, \
open(sample_middle, "w") as f_m, \
open(sample_end, "w") as f_e:
f_b.write(data)
f_b.write(separator)
for i in range(rows):
if not i % middle_row:
f_m.write(data)
f_m.write(separator)
for t in (f_b, f_m, f_e):
t.write(",".join((str(random.random()) for _ in range(columns))))
t.write(separator)
f_e.write(data)
f_e.write(separator)
return ("beginning", sample_beginning), ("middle", sample_middle), ("end", sample_end)
def normalize_field(field):
field = field.lower()
while True:
s_index = field.find('(')
e_index = field.find(')')
if s_index == -1 or e_index == -1:
break
field = field[:s_index] + field[e_index + 1:]
return "_".join(field.split())
def encode_csv_field(field):
if isinstance(field, (int, float)):
field = str(field)
escape = False
if '"' in field:
escape = True
field = field.replace('"', '""')
elif "," in field or "\n" in field:
escape = True
if escape:
return ('"' + field + '"').encode("utf-8")
return field.encode("utf-8")
if __name__ == "__main__":
print("Generating sample data...")
start_time = time.time()
samples = generate_samples(os.getcwd(), "REMOVE THIS LINE", SAMPLE_ROWS)
print("Done, generation took: {:2} seconds.".format(time.time() - start_time))
print("Beginning tests...")
search_string = "REMOVE"
header = None
results = []
for f in ("temp_file_stream", "temp_file_wm",
"in_place_stream", "in_place_wm", "in_place_mmap"):
for s, path in samples:
for test in range(TEST_LOOPS):
result = collections.OrderedDict((("function", f), ("sample", s),
("test", test)))
print("Running {function} test, {sample} #{test}...".format(**result))
temp_sample = get_temporary_path(path)
shutil.copy(path, temp_sample)
print(" Clearing caches...")
subprocess.call(["sudo", "/usr/bin/sync"], stdout=DEV_NULL)
with open("/proc/sys/vm/drop_caches", "w") as dc:
dc.write("3\n") # free pagecache, inodes, dentries...
# you can add more cache clearing/invalidating calls here...
print(" Removing a line starting with `{}`...".format(search_string))
out = subprocess.check_output(["sudo", "chrt", "-f", "99",
"/usr/bin/time", "--verbose",
sys.executable, CALL_SCRIPT, temp_sample,
search_string, f], stderr=subprocess.STDOUT)
print(" Cleaning up...")
os.remove(temp_sample)
for line in out.decode("utf-8").split("\n"):
pair = line.strip().rsplit(": ", 1)
if len(pair) >= 2:
result[normalize_field(pair[0].strip())] = pair[1].strip()
results.append(result)
if not header: # store the header for later reference
header = result.keys()
print("Cleaning up sample data...")
for s, path in samples:
os.remove(path)
output_file = sys.argv[1] if len(sys.argv) > 1 else "results.csv"
output_results = os.path.join(os.getcwd(), output_file)
print("All tests completed, writing results to: " + output_results)
with open(output_results, "wb") as f:
f.write(b",".join(encode_csv_field(k) for k in header) + b"\n")
for result in results:
f.write(b",".join(encode_csv_field(v) for v in result.values()) + b"\n")
print("All done.")
最后(和 TL;DR):这是我的结果 - 我只从结果集中提取最佳时间和内存数据,但您可以在此处获得完整的结果集:@987654321 @ 和Python 3.6 Raw Test Data。
根据我收集的数据,最后几点说明:
- 如果工作内存是个问题(处理异常大的文件等),只有
*_stream 函数占用空间小。在 Python 3.x 中,mmap 技术是一种中间方式。
- 如果存储是一个问题,只有
in_place_* 函数是可行的。
- 如果两者都稀缺,唯一一致的技术是
in_place_stream,但代价是处理时间和增加的 I/O 调用(与 *_wm 函数相比)。
-
in_place_* 函数很危险,因为如果中途停止它们可能会导致数据损坏。 temp_file_* 函数(没有完整性检查)仅在非事务性文件系统上是危险的。