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

后端 未结 6 1088
甜味超标
甜味超标 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 08:18

    No need to use that many variables.

    with open(outfilename, 'w') as outfile:
        for fname in filenames:
            with open(fname, 'r') as readfile:
                outfile.write(readfile.read() + "\n\n")
    

提交回复
热议问题