[英]What causes my code to inflate text file size?
我编写了一个Python程序来遍历目录中的文本文件,并为每个版本创建新版本并添加行号。 这是程序中的相关功能:
def create_lined_ver(filename):
new_text = []
with open(filename + ".txt", "r+") as f:
text = f.readlines()
for (num, line) in enumerate(text):
new_text.append("[{0}]: ".format(num) + line)
with open(filename + "_lined" + ".txt", "a+") as f:
for line in new_text:
f.write(line)
为了测试它,我在一批文本文件上运行它,然后出于好奇,再次运行它(向已经编号的文件添加第二组行号)。 我注意到,每次运行该程序时,新创建的文件的文件大小都比在每行添加〜5-6个字符时应有的大小大得多。 对于每个后续运行,文件大小从150 KB(原始)增加到700、1800,然后是3000 KB。
是什么导致文件大小增加太多?
在第9行中,使用“ a +”标志打开文件。 这使文件可用于附加和读取。 有关打开命令的不同模式的说明,请参见此处 。 通过以“ w”模式打开文件,您将覆盖现有文件。
如前所述,在注释中,每次运行代码时,您都将附加到行内版本。 而是尝试:
def create_lined_ver(filename):
with open(filename + ".txt", "r") as f:
text = f.readlines()
new_text = ["[{0}]: ".format(num) + line for (num, line) in enumerate(text)]
with open(filename + "_lined" + ".txt", "w") as f:
f.write(''.join([new_text]))
我认为您不需要使用列表或附加到文件。
您正在寻找这样的东西。
def create_lined_ver(filename):
with open(filename + ".txt") as f_in, open(filename + " _lined.txt", "w") as f_out:
for num, line in enumerate(f_in):
f_out.write("[{}]: {}\n".format(num, line))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.