Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is running a bash command to copy paste a file in Python3 a reliable option?

Copying a file in Python3 using the following code takes a lot of time:

shutil.copy(self.file, self.working_dir)

However, the cp command of Linux is pretty fast. If I try to execute the bash command from Python3 for copying files with sizes greater than 100GB, will that be a reliable option for production servers?

I have seen this answer but its suggestions are not fast.

like image 697
Aviral Srivastava Avatar asked May 28 '26 05:05

Aviral Srivastava


1 Answers

If you are running on Windows, Python's copy buffer size may be too small: https://stackoverflow.com/a/28584857/679240

You would need to implement something similar to this (warning: untested):

def copyfile_largebuffer(src, dst, length=16*1024*1024):
    with open(newfile, 'wb') as outfile, open(oldfile, 'rb') as infile:
        copyfileobj_largebuffer(infile, outfile, length=length)

def copyfileobj_largebuffer(fsrc, fdst, length=16*1024*1024):
    while 1:
        buf = fsrc.read(length)
        if not buf:
            break
        fdst.write(buf)
like image 158
Haroldo_OK Avatar answered May 30 '26 19:05

Haroldo_OK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!