开发者

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

开发者 https://www.devze.com 2023-04-03 09:20 出处:网络 作者: Sir
本文实现文件分类器的目的主要是为了将办公过程中产生的各种格式的文件完成整理。

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

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

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

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

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

#Itimportstheosmodule.
importos

#Shutilisamodulethatprovidesanumberofhigh-leveloperationsonfilesandcollectionsoffiles.
importshutil

#Theglobmod编程客栈ulefindsallthepathnamesmatchingASPecifiedpatternaccordingtotherulesusedbytheUnixshell,
#althoughresultsarereturnedinarbitrarjsyorder.Notildeexpansionisdone,but*,?,andcharacterrangesexpressed
#with[]willbecorrectlymatched.
importglob
importsys

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

#Askingtheusertoinputthepathofthedirectorythatcontainsthefilestobesortedpython.
uncatched_dir=input('请输入待分类的文件路径:\n')

#Itchecksiftheuncatched_dirisempty.
ifuncatched_dir.strip()=='':
print('待分类的文件夹路径不能为空!')
sys.exit()

#Askingtheusertoinputthepathofthedirectorythatcontainsthefilestobesorted.
target_dir=input('请输入分类后文件存放的目标路径:\n')

#Itchecksifthetarget_dirisempty.
iftarget_dir.strip()=='':
print('分类后的文件存放路径不能为空!')
sys.exit()

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

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

#Itchecksifthetarget_direxists.Ifitdoesnotexist,itcreatesanewdirectoryinthecurrentworkingdirectory.
ifnotos.path.exists(target_dir):
#Itcreatesanewdirectoryinthecurrentworkingdirectory.
os.mkdir(target_dir)

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

#Avariablethatisusedtocountthenumberoffilesthathavebeenmoved.
file_move_num=0

#Avariablethatisusedtocountthenumberofnewdirectoriesthathavebeencreated.
dir_new_num=0

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

#Aforloopthatiteratesthroughallthefilesintheuncatched_dirdirectory.
forfile_inglob.glob(f'{uncatched_dir}/*开发者_C教程*/*',recursive=True):

#Itchecksifthefileisafile.
ifos.path.isfile(file_):

#Itgetsthefilenameofthefile.
file_name=os.path.basename(file_)

#Checkingifthefilenamecontainsaperiod.
if'.'infile_name:

#Gettingthesuffixofthefile.
suffix_name=file_name.split('.')[-1]

else:

#Usedtoclassifyfilesthatdonothaveasuffix.
suffix_name='others'

#I编程tchecksifthedirectoryexists.Ifitdoesnotexist,itcreatesanewdirectoryinthecurrentworking
#directory.
ifnotos.path.exists(f'{target_dir}/{suffix_name}'):

#Itcreatesanewdirectoryinthecurrentworkingdirectory.
os.mkdir(f'{target_dir}/{suffix_name}')

#Adding1tothevariabledir_new_num.
dir_new_num+=1

#Itcopiesthefiletothetargetdirectory.
shutil.copy(file_,f'{target_dir}/{suffix_name}')

#Adding1tothevariablefile_move_num.
file_move_num+=1

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

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

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

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

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

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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消