Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print character while copying big folder

Tags:

python

shutil

I have this function trying to print additional '.' every second while it's copying a big folder (~3GB) from one place to another:

def copy_folder(source, destination):
    print 'copying',
    while shutil.copytree(src=source, dst=destination):
        print '.',
        time.sleep(1)

but when I call the function:

source = 'source_folder'
destination = 'destination_folder'
copy_folder(source=source, destination=destination)

it's copying perfectly fine the whole folder but it does NOT print '.' at all.

Do I need to use threads?

like image 535
mawueth Avatar asked Dec 01 '25 09:12

mawueth


2 Answers

Threading in Python is pretty simple:

import sys, shutil, time, threading

class CopyThread(threading.Thread):
    def __init__(self, source, destination):
        super(CopyThread, self).__init__()

        self.source = source
        self.destination = destination

    def run(self):
        time.sleep(5)  # Delete me later on, I'm just here to slow things down
        return shutil.copytree(src=self.source, dst=self.destination)

if __name__ == '__main__':
    thread = CopyThread('source_folder', 'destination_folder')
    thread.start()

    while thread.is_alive():
        sys.stdout.write('.')
        sys.stdout.flush()

        time.sleep(1)

    thread.join()

Just subclass threading.Thread and override run(). After that, call .start() on an instance of that class and you have a thread.

like image 65
Blender Avatar answered Dec 04 '25 00:12

Blender


copytree will copy the entire tree so the while loop won't run until the copy is complete and the return value can be evaluated.

My python is rusty, but I'll give an idea of how to implement the thread (inspired by the code found here).

def copy_folder(source, destination):
    self.iscopying = True        
    self.thread = threading.Thread(name="GPS Data", target=self.thread_run)
    self.thread.setDaemon(True)
    self.thread.start()
    shutil.copytree(src=source, dst=destination)
    self.iscopying = false


def thread_run(self):
    while self.iscopying:
        print '.'
        time.sleep(1)

Essentially, create a flag that tells the thread when the copying is happening and then set it to false when the copy is complete.

like image 30
brianestey Avatar answered Dec 03 '25 23:12

brianestey



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!