i have written following script concatenate files in directory 1 single file.
can optimized, in terms of
idiomatic python
time
here snippet:
import time, glob outfilename = 'all_' + str((int(time.time()))) + ".txt" filenames = glob.glob('*.txt') open(outfilename, 'wb') outfile: fname in filenames: open(fname, 'r') readfile: infile = readfile.read() line in infile: outfile.write(line) outfile.write("\n\n")
use shutil.copyfileobj copy data:
import shutil open(outfilename, 'wb') outfile: filename in glob.glob('*.txt'): if filename == outfilename: # don't want copy output output continue open(filename, 'rb') readfile: shutil.copyfileobj(readfile, outfile) shutil reads readfile object in chunks, writing them outfile fileobject directly. not use readline() or iteration buffer, since not need overhead of finding line endings.
use same mode both reading , writing; important when using python 3; i've used binary mode both here.
Comments
Post a Comment