Showing posts with label except. Show all posts
Showing posts with label except. Show all posts

Thursday, October 1, 2009

Python - using glob to get lists of files and directories

import os.path
# glob is a simple and useful python module. 
# It uses simple regular expressions to match 
# directories and files for a given path.  If 
# you've ever used the command line to 'ls' or 
# 'dir' the currently directory you may be aware 
# that the directory accepts * or ? or [] to 
# match patterns.  glob is a python implementation 
# of this functionality. 
 
import glob
import os
 
# find all the .txt files in the current working directory  
print glob.glob('*.TXT')
# output: 
# ['LICENSE.txt', 'NEWS.txt', 'README.txt'] 
 
# you can also specify a full path 
# Here I'm searching for dll files in python 2.6 
print glob.glob('C:\Python26\DLLs\*.dll')
# output: 
#    ['C:\\Python26\\DLLs\\sqlite3.dll', 
#    'C:\\Python26\\DLLs\\tcl85.dll', 
#    'C:\\Python26\\DLLs\\tclpip85.dll', 
#    'C:\\Python26\\DLLs\\tk85.dll'] 
 
# If you are expecting a great deal of results 
# you should use the glob.iglob method that returns 
# matches as it goes and does not load everything 
# into memory first. 
# glob.iglob() example 
f = glob.iglob('C:\Python26\Lib\*')
 
spitItOut = True
while spitItOut:
    try:
        fileNameAndPath = f.next()
        # since glob gives you the full path you can 
        # use the output with some of the os module's methods 
        if os.path.isfile(fileNameAndPath):
            fileNameAndPath += " is a file." 
        else:
            fileNameAndPath += " is not a file." 
        print fileNameAndPath
    except StopIteration:
        spitItOut = False
 
#output (snipped a bit...since there a lot): 
#    C:\Python26\Lib\abc.py is a file. 
#    ....[snip] 
#    C:\Python26\Lib\compiler is not a file. 
#    ...[another snip] 
#    C:\Python26\Lib\getopt.py is a file. 
#    C:\Python26\Lib\getopt.pyc is a file. 
#    C:\Python26\Lib\getpass.py is a file. 
#    C:\Python26\Lib\gettext.py is a file. 
#    C:\Python26\Lib\glob.py is a file. 
#    C:\Python26\Lib\glob.pyc is a file. 
 
 

Saturday, September 19, 2009

Python - try except - error handling in python

import sys

# simple error catch
try:
    # an obvious error
    a = 1/0
except ZeroDivisionError as err:
    print "Specific Exception: ", err
    print ""

# catch one of several specific
try:
    a = 1/0
except (ZeroDivisionError, ValueError, IOError) as err:
    print "One of several Exceptions: ", err
    print ""
   
# catch all exceptions
try:
    a = 1/0
except:
    print "Generic Catchall"
    for i in sys.exc_info():
        print i
    print ""

# you can also do a combo
try:
    a = 1/0
except ZeroDivisionError as err:
    print "Combo: ", err
    print ""
except (ZeroDivisionError, ValueError, IOError) as err:
    print "Combo: One of several Exceptions: ", err
    print ""
except:
    print "Combo: Generic Catchall"
    for i in sys.exc_info():
        print i

       
## output:
##Specific Exception:  integer division or modulo by zero
##
##One of several Exceptions:  integer division or modulo by zero
##
##Generic Catchall
##
##integer division or modulo by zero
##
##
##Combo:  integer division or modulo by zero