Advertisement
Guest User

SearcPic.py

a guest
Aug 9th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: iso-8859-1 -*-
  3. import os, re, sys, fnmatch
  4. import os.path as Path
  5.  
  6. def main(cliargs):
  7.     rootdir, pattern, outfile= cliargs
  8.     HEADER='#Geeqie collection \n#created with Geeqie version 1.0 \
  9. \n#geometry: 25 95 440 450\n'
  10.     FOOTER='#END'
  11.     PATTERNS= [p for p in pattern.split(',')][:-1]
  12.     if len(PATTERNS) == 0: return
  13.     with open(outfile, 'w') as _outfile:
  14.         _outfile.write(HEADER)
  15.         for p in PATTERNS:
  16.             listfiles=Walk(rootdir,1,p)
  17.             for line in listfiles:
  18.                 _outfile.write('"'+line+'"\n')
  19.         _outfile.write(FOOTER+'\n')
  20.  
  21. def Exists(filepath):
  22.     return Path.isfile(filepath) or None
  23.  
  24. def Walk( root, recurse=0, pattern='*', return_folders=0 ):
  25.     # initialize
  26.     result = []
  27.     # must have at least root folder
  28.     try:
  29.         names = os.listdir(root)
  30.     except os.error:
  31.         return result
  32.     # expand pattern
  33.     pattern = pattern or '*'
  34.     # check each file
  35.     for name in names:
  36.         fullname = Path.normpath(Path.join(root, name))
  37.         # grab if it matches our pattern and entry type
  38.         for pat in pattern.split(';'):
  39.             if fnmatch.fnmatch(name, pat):
  40.                 if Exists(fullname) or (return_folders and Path.isdir(fullname)):
  41.                     result.append(fullname)
  42.                 continue
  43.         # recursively scan other folders, appending results
  44.         if recurse:
  45.             if Path.isdir(fullname) and not Path.islink(fullname):
  46.                 result = result + Walk( fullname, recurse, pattern, return_folders )
  47.     return result
  48.  
  49. if __name__ == '__main__':
  50.     #import pdb; pdb.set_trace()
  51.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement