Search code examples
pythonshutil

How to detect shutil.copytree is done copying?


I use python 3.8 on archlinux. I use shutil.copytree to copy a folder into a usb key. When i check into the usb after copy, the folder is correctly copied, but i see that my usb key that has a ticking led is still writing. If i eject my usb key before the ticking is over and reconnect it, i see that the folders are not entirely copied. Means that my OS (archlinux) thinks that the copy is done, but it's not. Any ideas ?

def copytree(self,src, dst):
    """
        This method copies an entire folder recusively from the src to dst path
    """

    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isdir(s):
            shutil.copytree(s, d)
        else:
            shutil.copy2(s, d)

And the code following this call goes on even if it didn't finish the copy.


Solution

  • It seems like the copy has logically finished, but hasn't been fully written to the physical USB flash drive. That is to say, your OS has cached some of the data from the copy, and will actually write it to the drive at a later time, but definitely before the drive is unmounted.

    To check if this is the case, you can check (either within Python or through some other program) if the copy has completed once the code has moved on past shutil.copytree. If it has, then the issue is almost certainly that the USB flash drive was not safely ejected.

    You should be able to solve this by unmounting (i.e. safely ejecting) the USB flash drive before removing it.