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

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

最终效果:
最终效果图

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

#--------------------【classify.py】------------------
#       author      :   zhyh2010
#       date        :   201505113
#       target      :  整理endnote自动下载的pdf文件,使其移动到一个文件夹中
#-------------------------【end】--------------------------

import os.path
import 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  缺少() 没有初始化self
instance = classify()                
instance.classify_file()

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

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

3.Python的startswith与endswith函数

4.Python shutil模块

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

6try..except

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值