简体   繁体   中英

Python : Copy content of directory c:\FLASH to all connected USB flash drives

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM