Extract files with a specific extension in a folder using Python
I don't know when the network is idle and useless. I hung up a Linux video tutorial, which contains many folders and many irrelevant files. This is simply intolerable for people who do not have to collect junk files. So I planned to extract them one by one manually and return them to a folder. There are too many helpless files, and there is really no spare time to extract them one by one. So I was planning to write a VC program. During the winter vacation at home, I suddenly lost my interest in the many windows APIs and a bunch of Functional Descriptions in English. It is true that you are too lazy to study independently.
In the past few days, I have learned the boost library and found a font written for python, which has aroused my desire for python for the semester, I mainly want to see the advantages of combining these two languages. Google once said that python core programming is good, but the quality of translation is not very good. I remember seeing it on a blog of Daniel that the translation quality is poor. I 'd rather not read it than read the original version. So I ran to the Amazon website and found a five-star book, <beginning Python using Python 2.6 and Python 3.1-James W. payne>, print it out and start my python journey.
Fortunately, I saw a lot in msdn before, and I was barely able to get through English. What do I fear when I added the powerful Google translation. After reading the basic syntax in one breath, I think it is strange that the syntax is less than 200 pages, and the basic types, functions, exceptions, classes, and OOP can be completed even if there are less than 200 pages. This is a big gap between C ++ and me. The C ++ syntax can really torture you. Later, I borrowed the core Python programming and found that the syntax over there was much more detailed.
So the book has been on the verge of battles over the past few days.
After talking about that, let's just return to the topic. This program is actually very simple, without regular expressions (it is really a good thing, boost makes C ++ also supports regular expressions, haha, but I have not started learning, it is also very easy. No, read the program.
View plaincopy to clipboardprint?
01 ."""
02. This module is to copy file with the specific extension from one directory
03. To the specific directory
04 ."""
05.
06. Import OS
07. Import shutil
08.
09. Def file_extension (frompath, extension, topath ):
10. If OS. Path. isdir (frompath ):
11. fileall = OS. listdir (frompath)
12. For each in fileall:
13. absolutepath = OS. Path. Join (frompath, each)
14. If OS. Path. isdir (absolutepath ):
15. file_extension (absolutepath, extension, topath)
16. Else:
17. If absolutepath. endswith (Extension ):
18. Print absolutepath
19. shutil. Copy (absolutepath, topath)
20. Else:
21. Print "Please input correct path !!! "
"""
This module is to copy file with the specific extension from one directory
To the specific directory
"""
Import OS
Import shutil
Def file_extension (frompath, extension, topath ):
If OS. Path. isdir (frompath ):
Fileall = OS. listdir (frompath)
For each in fileall:
Absolutepath = OS. Path. Join (frompath, each)
If OS. Path. isdir (absolutepath ):
File_extension (absolutepath, extension, topath)
Else:
If absolutepath. endswith (Extension ):
Print absolutepath
Shutil. Copy (absolutepath, topath)
Else:
Print "Please input correct path !!! "
I don't need to comment it out. It's quite simple.
I started to learn python. This code must be ugly and I will try again later.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ruanmengkai/archive/2010/05/06/5564243.aspx