Endnote downloaded files placed in a PDF folder in a heap of scrambled numbers in the folder
Now the boss asked to download the files sent to him, but a copy of the feeling some eggs hurt, so think of Python, through the data, the perfect realization of the function of this finishing, is really a python dafa good 2333333333333
Final effect:
I python rookie, write bad place, please forgive me
#--------------------"classify.py"------------------# author:zhyh2010# date:201505113# Target: Organize endnote automatically downloaded PDF files and move them to a folder#-------------------------"End"--------------------------ImportOs.pathImportShutil#-------------------------"class classify"--------------------------# Para:curpath Current Path# Extension Extension# Target_dir Destination Storage folder# Three member variables are initialized in Init, and modifying these three parameters in Init will enable the extension of the function.#-------------------------"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: Traverse the path of the path file and move the PDF file in the folder to the appropriate directory #-------------------------"Classify_file End"-------------------------- def classify_file(self):Extension = self.extension Path = Self.curpathif notOs.path.isdir (PATH):# Determine if the path isPrint' error! It is not a dir ')returnSelf.makedir () forRoot, dirs, listinchOs.walk (PATH): forFileinchListifFile.endswith (extension): Print (' moving\t '+ file +' The \ting ... ' .... ')Try: Self.movefiles (Os.path.join (root, file))except:ContinuePrint'-------------------done-----------------')#-------------------------"MakeDir"-------------------------- # Target: Create destination folder #-------------------------"MakeDir End"--------------------- def makedir(self):Target_dir = Self.target_dirif notOs.path.exists (Target_dir): Os.mkdir (Target_dir)#-------------------------"Movefiles"-------------------------- # target: Move files to file directory #-------------------------"Movefiles End"--------------------- def movefiles(self, file):Target_dir = Self.target_dir shutil.copy (file, Target_dir)#instance = classify missing () does not initialize selfInstance = classify () Instance.classify_file ()
Resources
1.Python notes-Class definition
2.Python file Operations and folder traversal
StartsWith and EndsWith functions of 3.Python
4.Python Shutil Module
5python determine if files and folders exist
6try.. Except
Python implementation of endnote download PDF file