Windows 10 桌面文件自动整理工具 - Python 代码实现
Windows 10 桌面文件自动整理工具 - Python 代码实现
想要一个能够打开并自动整理桌面文件的程序?使用 Python 编程语言可以轻松实现!
以下是一个示例的 Python 代码:
import os
import shutil
# 获取桌面路径
desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
# 创建文件夹用于整理文件
folders = ['Documents', 'Pictures', 'Videos', 'Music', 'Others']
for folder in folders:
folder_path = os.path.join(desktop_path, folder)
os.makedirs(folder_path, exist_ok=True)
# 遍历桌面文件
files = os.listdir(desktop_path)
for file in files:
file_path = os.path.join(desktop_path, file)
if os.path.isfile(file_path):
file_extension = os.path.splitext(file)[1]
# 将文件移动到相应的文件夹
if file_extension.lower() in ['.doc', '.docx', '.txt', '.pdf']:
destination_folder = os.path.join(desktop_path, 'Documents')
elif file_extension.lower() in ['.jpg', '.jpeg', '.png', '.gif']:
destination_folder = os.path.join(desktop_path, 'Pictures')
elif file_extension.lower() in ['.mp4', '.avi', '.mov']:
destination_folder = os.path.join(desktop_path, 'Videos')
elif file_extension.lower() in ['.mp3', '.wav']:
destination_folder = os.path.join(desktop_path, 'Music')
else:
destination_folder = os.path.join(desktop_path, 'Others')
shutil.move(file_path, os.path.join(destination_folder, file))
将上述代码保存为一个 Python 文件(例如desktop_organizer.py),然后在 Windows 10 系统中运行 Python 解释器来执行该程序。执行后,程序将遍历桌面上的文件,并根据文件类型将其移动到相应的文件夹中(Documents、Pictures、Videos、Music、Others)。
注意:
- 运行此程序之前,请确保备份或移走桌面上的文件,以免不小心将其整理到错误的文件夹中。
- 此程序并不会处理桌面上的文件夹,只会处理桌面上的文件。
希望这个 Python 代码能帮助您轻松整理 Windows 10 桌面的文件!
原文地址: https://www.cveoy.top/t/topic/qnL8 著作权归作者所有。请勿转载和采集!