Created
September 8, 2012 09:15
-
-
Save chaomai/3672961 to your computer and use it in GitHub Desktop.
Python 3:get specific type files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#scan a directory iteratively and get specific type files | |
#tested under Python3 | |
import os | |
import os.path | |
def print_filename(item, sou_dir, type): | |
(filepath, filename) = os.path.split(item) | |
if type == os.path.splitext(filename)[1]: | |
sourcefile = os.path.join(sou_dir, item) | |
print(sourcefile) | |
else: | |
return | |
def find(sou_dir, type): | |
for item in os.listdir(sou_dir): | |
subdir = os.path.join(sou_dir, item) | |
if os.path.isfile(subdir): | |
print_filename(item, sou_dir, type) | |
else: | |
find(subdir, type) | |
if __name__ == '__main__': | |
source = input('source directory:') | |
type = input('filetype:') | |
find(source, type) | |
print('Done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment