当前位置: 首页 > news >正文

python文件自动分类(5)

完成了文件自动分类的操作后,我们一起来复习下:

首先,获取文件夹中所有文件名称,用 os.path.join() 函数拼接出要移动到的目标地址。然后,使用 os.path.exists() 函数判断目标文件夹是否存在,不存在用 os.mkdir() 创建文件夹。再者,使用 os.path.isdir() 函数判断是否为文件夹。最后,当不是文件夹时,使用 shutil.move() 函数移动。

# 使用import导入os模块

import os

# 使用import导入shutil模块

import shutil

# 将阿文的下载文件夹路径 /Users/yequ/Desktop 赋值给变量desktopPath

desktopPath = "/Users/yequ/Desktop"

# 使用os.listdir()函数获取该路径下所有的文件(夹),并赋值给变量allItems

allItems = os.listdir(desktopPath)

# 使用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(desktopPath, "图片文件")

    elif extension in [".avi", ".mp4", ".wmv", ".mov", ".flv"]:

        # 使用os.path.join()函数拼接分类文件夹路径:视频文件

        # 并赋值给变量targetPath

        targetPath = os.path.join(desktopPath, "视频文件")

    elif extension in [".wav", ".mp3", ".mid", ".ape", ".flac"]:

        # 使用os.path.join()函数拼接分类文件夹路径:音频文件

        targetPath = os.path.join(desktopPath, "音频文件")

    elif extension in [".pdf"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PDF文件

        targetPath = os.path.join(desktopPath, "PDF文件")

    elif extension in [".docx", ".doc"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Word文件

        targetPath = os.path.join(desktopPath, "Word文件")

    elif extension in [".xlsx", ".xls"]:

        # 使用os.path.join()函数拼接分类文件夹路径:Excel文件

        targetPath = os.path.join(desktopPath, "Excel文件")

    elif extension in [".pptx", ".ppt"]:

        # 使用os.path.join()函数拼接分类文件夹路径:PPT文件

        targetPath = os.path.join(desktopPath, "PPT文件")

    else:

        # 使用os.path.join()函数拼接分类文件夹路径:其他文件

        targetPath = os.path.join(desktopPath, "其他文件")

    # 判断当如果目标文件夹不存在

    if not os.path.exists(targetPath):

        # 使用os.mkdir()函数创建文件夹

        os.mkdir(targetPath)

    # 使用os.path.join()函数拼接desktopPath和文件名

    # 并赋值给变量itemPath

    itemPath = os.path.join(desktopPath, item)

    # 判断当itemPath不是文件夹时。

    if not os.path.isdir(itemPath):

        # 使用shutil.move()函数移动文件到targetPath路径

        shutil.move(itemPath, targetPath)

http://www.lryc.cn/news/431312.html

相关文章:

  • 【Unity-Lua】音乐播放器循环滚动播放音乐名
  • 宏碁扩展Swift系列,推出四款全新AI笔记本电脑
  • 科研绘图系列:R语言差异基因四分图(Quad plot)
  • 文字或图案点选坐标点返回
  • 硬盘数据恢复软件TOP4榜单出炉,选对方法竟然如此重要
  • 给自己复盘用的随想录笔记-栈与队列
  • 微信小程序跳转到另一个微信小程序
  • 【知识图谱】4、LLM大模型结合neo4j图数据库实现AI问答的功能
  • 《信息技术 云计算 边缘云通用技术要求》国家标准发布,九州未来参编
  • NTFS硬盘支持工具Paragon NTFS for Mac 15.4.44 中文破解版
  • 66-java 类型擦除
  • 25考研人数预计下降?这一届考研有哪些新趋势?
  • 比尔·盖茨对AI充满信心
  • Selenium 实现图片验证码识别
  • 基于云原生向量数据库 PieCloudVector 的 RAG 实践
  • 内存泄漏的影响
  • shell变量扩展你知道多少?
  • Compose中对于KeyEvent的处理
  • OpenXR Monado compositor处理应用layers(cheduled->delivered)
  • leetcode:1137 Tribonacci 数列
  • 简单讲一下API的作用以及介绍
  • 猎板道出PCB免费打样真相:制造成本究竟给了谁?
  • Linux 竞争与并发(学习总结)
  • SaaS初创企业需求建模指南
  • MySQL最左匹配原则
  • 日常开发1:居中处理
  • css弹性盒子——flex布局
  • 亚马逊云科技 Gen BI 2024-09-04 上海站QuickSight
  • 【Qt】Qt和JavaScript使用QWebChannel交互
  • 码住!15个爆好用知识库软件工具分享