Home > other >  python3 PermissionError: [Errno 13] Permission denied:
python3 PermissionError: [Errno 13] Permission denied:

Time:10-16

Try to walk through entire C:\ hard drive and take copy of all .jpg .mp3, mp4 and avi files from windows hard drive to abc folder on desktop or USB flash drive before formatting hard drive and clean installation windows.

import os.path
import shutil

#Create Directory if don't exist in Desktop path
dir_name = "abc"
dir_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
#dir_path = os.path.expanduser("~/Desktop")
file_path = os.path.join(dir_path, dir_name)

if not os.path.exists(file_path):
    os.mkdir(file_path)
    print(file_path)

  
extensions = [".mp3", ".mpeg", ".mp4", ".jpg", ".jpeg", ".avi"]

src_path = r"C:\\"
dst_dir = file_path
for root, dirs,files in os.walk(src_path):
   for file in extensions:
      if file.endswith(tuple(extensions)):
         shutil.copy(file_path, dst_dir)

CodePudding user response:

src_path = r'C:\\', encoding='utf-8'

or

chmod 777 "your file"

CodePudding user response:

I solved it, but now I need to know how to connect to the USB flash drive. For example - if the USB flash drive is connected to D:/ - then copy files, if the USB flash drive is connected to F:/ - then copy files. Or is there another way that Python can automatically detect a USB flash drive and copy files?

import os
import shutil

#Create Directory if don't exist in Desktop path
dir_name = "abc"

dir_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
#dir_path = os.path.expanduser("~/Desktop")
file_path = os.path.join(dir_path, dir_name)

if not os.path.exists(file_path):
    os.mkdir(file_path)
    print(file_path)

path = r'C:\\'
extensions = [".png", ".mp3", ".mpeg", ".mp4", ".jpg", ".jpeg", ".avi"]
for root, dir, files in os.walk(path):
    for file in files:
        if file.endswith(tuple(extensions)):
            shutil.copy2(os.path.join(root, file), file_path)
            print(f"File{file}")
  • Related