python script to concatenate all the files in the directory into one file

后端 未结 6 1079
甜味超标
甜味超标 2020-12-15 07:55

I have written the following script to concatenate all the files in the directory into one single file.

Can this be optimized, in terms of

  1. idiomat

6条回答
  •  借酒劲吻你
    2020-12-15 08:07

    Using Python 2.7, I did some "benchmark" testing of

    outfile.write(infile.read())
    

    vs

    shutil.copyfileobj(readfile, outfile)
    

    I iterated over 20 .txt files ranging in size from 63 MB to 313 MB with a joint file size of ~ 2.6 GB. In both methods, normal read mode performed better than binary read mode and shutil.copyfileobj was generally faster than outfile.write.

    When comparing the worst combination (outfile.write, binary mode) with the best combination (shutil.copyfileobj, normal read mode), the difference was quite significant:

    outfile.write, binary mode: 43 seconds, on average.
    
    shutil.copyfileobj, normal mode: 27 seconds, on average.
    

    The outfile had a final size of 2620 MB in normal read mode vs 2578 MB in binary read mode.

提交回复
热议问题