Advertisement
goatbar

trying out listdir and fnmatch

Feb 5th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. # Variations on listdir and fnmatch
  2.  
  3. from fnmatch import fnmatch
  4. from glob import glob
  5. import os
  6. import subprocess
  7.  
  8. print 'glob:', glob('g*.py')
  9.  
  10. for n, filename in enumerate([f for f in os.listdir('.') if fnmatch(f, '*.py')]):
  11.     print n, filename
  12.  
  13. files_to_relocate = [f for f in os.listdir('.') if fnmatch(f, '*.py')]
  14. for n, filename in enumerate(files_to_relocate):
  15.   print n, filename
  16.   # I prefer subprocess over system or exec.
  17.   #result = subprocess.call(['reloc', str(n), filename])
  18.   #if 0 != result:
  19.   #  print 'failed with exit code > 0', result
  20.  
  21. for dirpath, dirnames, filenames in os.walk('.'):
  22.   filenames = [name for name in filenames if fnmatch(name, '*.py')]
  23.   print dirpath, filenames
  24.   for filename in filenames:
  25.     print 'filepath:', os.path.join(dirpath, filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement