将特定文件从驱动器 C: 复制到桌面上的文件夹
Copy specific files from drive C: to a folder on Desktop
我需要遍历整个 C:\ 硬盘并将所有 .jpg .mp3、mp4 和 .avi 文件从 Windows 硬盘 C: 复制到桌面上的备份文件夹
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)
src_path = r'C:\', encoding='utf-8'
或
chmod 777“你的文件”
你必须写:
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)
我需要遍历整个 C:\ 硬盘并将所有 .jpg .mp3、mp4 和 .avi 文件从 Windows 硬盘 C: 复制到桌面上的备份文件夹
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)
src_path = r'C:\', encoding='utf-8'
或
chmod 777“你的文件”
你必须写:
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)