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.