Lamfai

一、如何按文件格式自动归类到文件夹?

  1、如何移动文件?

  使用内置模块来实现,shutil.move

  2、归类的规则是什么?

  手动预设文件夹 ×

  自动创建文件夹 √

  3、需要处理的素材(为了方便运行,这份代码文件也在素材文件夹里):https://files.cnblogs.com/files/Lamfai/script_project2_files.zip

import os
import shutil

path = \'./\'
files = os.listdir(path)

for f in files:
    folder_name = \'./\' + f.split(\'.\')[-1]

    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
        shutil.move(f, folder_name)
    else:
        shutil.move(f, folder_name)

  分类前:

  

  分类后:

  

 二、如何实现文件分类

  文件素材链接:https://files.cnblogs.com/files/Lamfai/problem2_files.zip

  使用 Python 进行这样的操作: 

  1. 把 jpg,png,gif 文件夹中的所有文件移动到 image 文件夹中,然后删除 jpg,png,gif 文件夹 

  2. 把 doc,docx,md,ppt 文件夹中的所有文件移动到 document 文件夹中,然后删除

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-04-27
  • 2021-09-15
  • 2021-07-31
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-06-18
猜你喜欢
  • 2021-08-30
  • 2022-01-04
  • 2021-10-24
  • 2022-01-03
  • 2021-09-22
  • 2021-08-18
相关资源
相似解决方案