码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-13 10:16:52      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:python   endnote   文件管理   

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

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

标签:python   endnote   文件管理   

原文地址:http://blog.csdn.net/zhyh1435589631/article/details/45688967

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!