You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: utf-8 -*-
# 使用import导入os,shutil模块
import os , shutil
# 从键盘输入文件路径 赋值给变量Path
Path = input("输入需要分类的文件夹目录:")
# 使用os.listdir()函数获取该路径下所有的文件(夹)并赋值给变量allItems
allItems = os.listdir(Path)
# 使用for循环遍历所有文件(夹)
for item in allItems:
# 获取文件后缀名
extension = os.path.splitext(item)[1].lower()
# 定义一个变量targetPath用来表示准备移动到的文件夹路径
targetPath = ""
if extension in [".jpg", ".jpeg", ".gif", ".png", ".bmp"]:
# 使用os.path.join()函数拼接分类文件夹路径:图片文件
# 并赋值给变量targetPath
targetPath = os.path.join(Path, "图片文件")
elif extension in [".avi", ".mp4", ".wmv", ".mov", ".flv"]:
# 使用os.path.join()函数拼接分类文件夹路径:视频文件
# 并赋值给变量targetPath
targetPath = os.path.join(Path, "视频文件")
elif extension in [".wav", ".mp3", ".mid", ".ape", ".flac","m4a"]:
# 使用os.path.join()函数拼接分类文件夹路径:音频文件
targetPath = os.path.join(Path, "音频文件")
elif extension in [".pdf"]:
# 使用os.path.join()函数拼接分类文件夹路径PDF文件
targetPath = os.path.join(Path, "PDF文件")
elif extension in [".docx", ".doc"]:
# 使用os.path.join()函数拼接分类文件夹路径Word文件
targetPath = os.path.join(Path, "Word文件")
elif extension in [".xlsx", ".xls"]:
# 使用os.path.join()函数拼接分类文件夹路径Excel文件
targetPath = os.path.join(Path, "Excel文件")
elif extension in [".pptx", ".ppt"]:
# 使用os.path.join()函数拼接分类文件夹路径PPT文件
targetPath = os.path.join(Path, "PPT文件")
else:
# 使用os.path.join()函数拼接分类文件夹路径:其他文件
targetPath = os.path.join(Path, "其他文件")
# 判断当如果目标文件夹不存在
if not os.path.exists(targetPath):
# 使用os.mkdir()函数创建文件夹
os.mkdir(targetPath)
# 使用os.path.join()函数拼接Path和文件名
# 并赋值给变量itemPath
itemPath = os.path.join(Path, item)
# 判断当itemPath不是文件夹时。
if not os.path.isdir(itemPath):
# 使用shutil.move()函数移动文件到targetPath路径
shutil.move(itemPath, targetPath)