Skip to content

Instantly share code, notes, and snippets.

@chaomai
Created September 8, 2012 09:15
Show Gist options
  • Save chaomai/3672961 to your computer and use it in GitHub Desktop.
Save chaomai/3672961 to your computer and use it in GitHub Desktop.
Python 3:get specific type files
#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