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

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

    I was curious to check more on performance and I used answers of Martijn Pieters and Stephen Miller.

    I tried binary and text modes with shutil and without shutil. I tried to merge 270 files.

    Text mode -

    def using_shutil_text(outfilename):
        with open(outfilename, 'w') 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, 'r') as readfile:
                    shutil.copyfileobj(readfile, outfile)
    
    def without_shutil_text(outfilename):
        with open(outfilename, 'w') 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, 'r') as readfile:
                    outfile.write(readfile.read())
    

    Binary mode -

    def using_shutil_text(outfilename):
        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)
    
    def without_shutil_text(outfilename):
        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:
                    outfile.write(readfile.read())
    

    Running times for binary mode -

    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    

    Running times for text mode -

    Shutil - 20.47757601737976
    Normal - 13.718038082122803
    

    Looks like in both modes, shutil performs same while text mode is faster than binary.

    OS: Mac OS 10.14 Mojave. Macbook Air 2017.

提交回复
热议问题