我编写了以下脚本来将目录中的所有文件连接成一个文件.
就此而言,这可以进行优化吗?
惯用的蟒蛇
时间
这是片段:
import time, glob
outfilename = 'all_' + str((int(time.time()))) + ".txt"
filenames = glob.glob('*.txt')
with open(outfilename, 'wb') as outfile:
for fname in filenames:
with open(fname, 'r') as readfile:
infile = readfile.read()
for line in infile:
outfile.write(line)
outfile.write("\n\n")
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 33
使用shutil.copyfileobj复制的数据:
import shutil
with open(outfilename, 'wb') as outfile:
for filename in glob.glob('*.txt'):
if filename == outfilename:
# don't want to copy the output into the output
continue
with open(filename, 'rb') as readfile:
shutil.copyfileobj(readfile, outfile)
Run Code Online (Sandbox Code Playgroud)
shutil从readfile块中读取对象,outfile直接将它们写入文件对象.不要使用readline()或迭代缓冲区,因为您不需要查找行结尾的开销.
使用相同的模式进行读写; 这在使用Python 3时尤为重要; 我在这里使用了二进制模式.