【Python 实战基础】如何自动整理文件夹

目录

一、实战场景

二、主要知识点

文件读写

基础语法

字符串处理

循环遍历

三、菜鸟实战

1、创建 python 文件

 2、运行结果


一、实战场景

实战场景:如何自动整理文件夹

二、主要知识点

  • 文件读写

  • 基础语法

  • 字符串处理

  • 循环遍历

三、菜鸟实战

马上安排!

1、创建 python 文件

"""
Author: 菜鸟实战
实战场景: 如何自动整理文件夹
"""

# 导入系统包
import platform
import os
import shutil

print("Hello,菜鸟实战")
print("实战场景: 如何自动整理文件夹 \n")
# 需要先创建文件夹resources
catalog = "resources"
# 读取resources目录下文件
for file in os.listdir(catalog):
    # 分割文件的后缀名
    ext = os.path.splitext(file)[1]
    ext = ext[1:]
    # 判断是否存在该目录
    if not os.path.isdir(f"{catalog}/{ext}"):
        os.mkdir(f"{catalog}/{ext}")
    # 将文件放到对应目录下
    source_path = f"{catalog}/{file}"
    target_path = f"{catalog}/{ext}/{file}"
    shutil.move(source_path, target_path)

print("资源整理完成!\n")
print("Python 版本", platform.python_version())

 2、运行结果

 菜鸟实战,持续学习!

猜你喜欢

转载自blog.csdn.net/qq_39816613/article/details/125338039
今日推荐