我正在将一堆文本文件合并到“merged.txt”中。然后我使用collections.counter来计算merged.txt中所有单词的数量。我想知道在我有mydict之后是否有办法删除文件merged.txt。或者只是一种方式,以便在代码执行后我的目录中没有merged.txt?
file_list = [file1, file2, ... file1000]
with open(directory + '/' + i +'/merged.txt', 'w') as outfile:
for fname in file_list:
with open(fname) as infile:
for line in infile:
outfile.write(line)
words = re.findall(r'\w+', open(directory + '/' + i + '/merged.txt').read().lower())
mydict = collections.Counter(words)
答案 0 :(得分:0)
import os
os.remove(file_to_remove)
删除文件。
或:
import shutil
shutil.move(file_to_move,destination).
移动它。