将指定文件从C:盘复制到Desktop的文件夹中



我需要遍寻整个C:硬盘,并将Windows硬盘C:中的所有。jpg .mp3, mp4和。avi文件拷贝到桌面的Backup文件夹

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)

你必须写:

shutil.copy2(os.path.join(root, file), file_path)
import os
import shutil
#Create Directory if don't exist in Desktop path
dir_name = "Backup"
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)

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

chmod 777 ';your file ';

相关内容

  • 没有找到相关文章

最新更新