python 实现endnote下载的pdf文件的整理

来源:互联网 发布:walk on water 知乎 编辑:程序博客网 时间:2024/05/22 12:57

endnote下载的文件放在一个pdf文件夹中的一堆乱序数字的文件夹中如图
endnote pdf文件夹
现在老板要求要把下载下来的文件发给他, 但一个个拷出来感觉有些蛋疼,于是就想到了python,通过查阅资料,完美实现了这个整理的功能,果然是python大法好2333333333333

最终效果:
最终效果图

本人python菜鸟,写的不好的地方,请见谅

#--------------------【classify.py】------------------#       author      :   zhyh2010#       date        :   201505113#       target      :  整理endnote自动下载的pdf文件,使其移动到一个文件夹中#-------------------------【end】--------------------------import os.pathimport shutil#-------------------------【class classify】--------------------------#    para : curpath         当前路径#           extension       扩展名#           target_dir      目标存储文件夹#       三个成员变量都在 init 中被初始化, 修改 init 中这三个参数就可以实现函数扩展了#-------------------------【class classify  end】--------------------------class classify:    curpath = ''    extension = ''    target_dir = ''    def __init__(self):        self.curpath = os.getcwd()        self.extension = '.pdf'        self.target_dir = 'endnote pdf'    #-------------------------【classify_file】--------------------------    #    target:   遍历 path 文件路径, 并将文件夹中的 pdf  文件移动至 相应目录中去    #-------------------------【classify_file  end】--------------------------    def classify_file(self):        extension = self.extension        path = self.curpath        if not os.path.isdir(path):         # 判断是否为路径            print('error! it is not a dir')            return        self.makedir()        for root, dirs, list in os.walk(path):            for file in list:                if file.endswith(extension):                    print('moving\t' + file + '\ting...............')                    try:                        self.movefiles(os.path.join(root, file))                    except:                        continue        print('-------------------done-----------------')    #-------------------------【makedir】--------------------------    #    target:   创建目标文件夹    #-------------------------【makedir  end】---------------------    def makedir(self):                target_dir = self.target_dir        if not os.path.exists(target_dir):            os.mkdir(target_dir)    #-------------------------【movefiles】--------------------------    #    target:   移动文件 到 file 目录下    #-------------------------【movefiles  end】---------------------    def movefiles(self, file):        target_dir = self.target_dir        shutil.copy(file, target_dir)#instance = classify  缺少() 没有初始化selfinstance = classify()                instance.classify_file()

参考资料
1.Python笔记——类定义

2.Python文件操作及文件夹遍历

3.Python的startswith与endswith函数

4.Python shutil模块

5python判断文件和文件夹是否存在

6try..except

0 0
原创粉丝点击