0

I need to copy some documents, folders and files from one directory on my local drive to all connected USB flash drives.

When destination is also folder everything works but in case the destination is USB drive root then I always get error:

Error: [WinError 5] Access denied: 'E:\\'

I think problem is in second backslash.

import wmi
import os
import shutil
import pathlib
import errno

src = pathlib.WindowsPath("c:/FLASH")

def clone(src, dst):
    try:
        shutil.copytree(src, dst)
    except OSError as e:
        if e.errno == errno.ENOTDIR:
            shutil.copy(src, dst)
        elif e.errno == errno.EACCES:
            print('Error: %s' % e)
    else:
        print('Error: %s' % e)


c = wmi.WMI()
for drive in c.Win32_LogicalDisk():
    print(drive.Caption, drive.Description)
    if drive.DriveType == 2:
        dst = pathlib.PureWindowsPath(drive.Caption, '\\')
        clone(src, dst)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
VACMA
  • 1
  • 1
  • I would try with / instead of \\ since using `print pathlib.PureWindowsPath('E:','/')` prints `E:\'. *But* also check if the device is not write protected and that you have proper writing rights etc – tgo Nov 01 '18 at 13:45
  • Hello, I tried it with '/' but result is exactly the same. It is without any problem in case I'm writing to some folder but writing to root is not possible. – VACMA Nov 01 '18 at 14:17

1 Answers1

0

I found this question randomly, The possible solution is:

def clone(src, dst, symlinks=False, ignore=None):
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, symlinks, ignore)
    else:
        shutil.copy2(s, d)

c = wmi.WMI()
for drive in c.Win32_LogicalDisk():
 print(drive.Caption, drive.Description)
 if drive.DriveType == 2:
    dst = pathlib.PureWindowsPath(drive.Caption, '\\')
    clone(src, dst)