基于Python实现文件分类器的示例代码

 更新时间:2023年04月02日 11:01:37   作者:Sir 老王  
这篇文章主要为大家详细介绍了如何基于Python实现文件分类器,目的主要是为了将办公过程中产生的各种格式的文件完成整理,感兴趣的可以了解一下

本文实现文件分类器的目的主要是为了将办公过程中产生的各种格式的文件完成整理。

通过自定义需要整理的文件目录,将该目录下面的全部文件按照文件格式完成分类操作。

实现逻辑使用的python技术栈就是os、glob、shutil三个标准库的综合运用,完成自动化的文件整理。

分别将这三个文件处理模块导入代码块中,进入后续的开发操作。

# It imports the os module.
import os

# Shutil is a module that provides a number of high-level operations on files and collections of files.
import shutil

# The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell,
# although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed
# with [] will be correctly matched.
import glob
import sys

将需要分类的文件目录uncatched_dir以及分类后文件存放目录target_dir设置为可以手动输入的方式。

# Asking the user to input the path of the directory that contains the files to be sorted.
uncatched_dir = input('请输入待分类的文件路径:\n')

# It checks if the uncatched_dir is empty.
if uncatched_dir.strip() == '':
    print('待分类的文件夹路径不能为空!')
    sys.exit()

# Asking the user to input the path of the directory that contains the files to be sorted.
target_dir = input('请输入分类后文件存放的目标路径:\n')

# It checks if the target_dir is empty.
if target_dir.strip() == '':
    print('分类后的文件存放路径不能为空!')
    sys.exit()

检验输入的分类后文件存放目录路径是否存在,因为很可能是输入一个新的路径,不存在时则新建一个该路径。

# It checks if the target_dir exists. If it does not exist, it creates a new directory in the current working directory.
if not os.path.exists(target_dir):
    # It creates a new directory in the current working directory.
    os.mkdir(target_dir)

定义一个文件移动数量的变量file_move_num,以及一个新建的文件夹数量的变量dir_new_num用于记录文件整理的结果记录。

# A variable that is used to count the number of files that have been moved.
file_move_num = 0

# A variable that is used to count the number of new directories that have been created.
dir_new_num = 0

遍历需要整理的文件夹目录uncatched_dir,对该目录下面的所有类型的文件进行自动整理操作。

# A for loop that iterates through all the files in the uncatched_dir directory.
for file_ in glob.glob(f'{uncatched_dir}/**/*', recursive=True):

    # It checks if the file is a file.
    if os.path.isfile(file_):

        # It gets the file name of the file.
        file_name = os.path.basename(file_)

        # Checking if the file name contains a period.
        if '.' in file_name:

            # Getting the suffix of the file.
            suffix_name = file_name.split('.')[-1]

        else:

            # Used to classify files that do not have a suffix.
            suffix_name = 'others'

        # It checks if the directory exists. If it does not exist, it creates a new directory in the current working
        # directory.
        if not os.path.exists(f'{target_dir}/{suffix_name}'):

            # It creates a new directory in the current working directory.
            os.mkdir(f'{target_dir}/{suffix_name}')

            # Adding 1 to the variable dir_new_num.
            dir_new_num += 1

        # It copies the file to the target directory.
        shutil.copy(file_, f'{target_dir}/{suffix_name}')

        # Adding 1 to the variable file_move_num.
        file_move_num += 1

注意:为了避免移动文件夹而造成的异常,尤其是系统盘,因此这里用的是复制,也就是shutil.copy函数使用。

最后,将文件分类数量、文件夹新建数量使用print函数进行打印即可。

print(f'整理完成,有{file_move_num}个文件分类到了{dir_new_num}个文件夹中!\n')

input('输入任意键关闭窗口...')

为了避免程序执行完成后直接将命令窗口关闭,上面使用了input函数来保持窗口暂停的效果。

到此这篇关于基于Python实现文件分类器的示例代码的文章就介绍到这了,更多相关Python文件分类器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • OpenCV 表盘指针自动读数的示例代码

    OpenCV 表盘指针自动读数的示例代码

    这篇文章主要介绍了OpenCV 表盘指针自动读数的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • 在Python中使用HTMLParser解析HTML的教程

    在Python中使用HTMLParser解析HTML的教程

    这篇文章主要介绍了在Python中使用HTMLParser解析HTML的教程,尤其是在用Python制作爬虫程序的时候经常可以用到,需要的朋友可以参考下
    2015-04-04
  • 史上最详细的Python打包成exe文件教程

    史上最详细的Python打包成exe文件教程

    这篇文章主要给大家介绍了关于Python打包成exe文件的相关资料,堪称史上最详细的教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • 在python3.9下如何安装scrapy的方法

    在python3.9下如何安装scrapy的方法

    这篇文章主要介绍了在python3.9下如何安装scrapy的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • python利用MethodType绑定方法到类示例代码

    python利用MethodType绑定方法到类示例代码

    这篇文章主要给大家介绍了关于python利用MethodType绑定方法到类的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
    2017-08-08
  • 利用Django提供的ModelForm增删改数据的方法

    利用Django提供的ModelForm增删改数据的方法

    这篇文章主要介绍了利用Django提供的ModelForm增删改数据,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • python实现文法左递归的消除方法

    python实现文法左递归的消除方法

    这篇文章主要介绍了python实现文法左递归的消除的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • 教你一分钟在win10终端成功安装Pytorch的方法步骤

    教你一分钟在win10终端成功安装Pytorch的方法步骤

    这篇文章主要介绍了教你一分钟在win10终端成功安装Pytorch的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • python中对开区间和闭区间的理解

    python中对开区间和闭区间的理解

    这篇文章主要介绍了python中对开区间和闭区间的理解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Python 实现文件读写、坐标寻址、查找替换功能

    Python 实现文件读写、坐标寻址、查找替换功能

    这篇文章主要介绍了Python 实现文件读写、坐标寻址、查找替换功能,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2019-09-09

最新评论