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

后端 未结 6 1084
甜味超标
甜味超标 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:17

    You can iterate over the lines of a file object directly, without reading the whole thing into memory:

    with open(fname, 'r') as readfile:
        for line in readfile:
            outfile.write(line)
    

提交回复
热议问题