Course Home Online Lessons
Related LinksOnline DevelopmentLessonsReferenceeBooks |
Regular ExpressionsThis material is from the Google Python Course In this section, we look at a few of Python's many standard utility modules to solve common problems. File System -- os, os.path, shutilThe *os* and *os.path* modules include many functions to interact with the file system. The *shutil* module can copy files.
## Example pulls filenames from a dir, prints their relative and absolute paths def printdir(dir): filenames = os.listdir(dir) for filename in filenames: print filename ## foo.txt print os.path.join(dir, filename) ## dir/foo.txt (relative to current dir) print os.path.abspath(os.path.join(dir, filename)) ## /home/nick/dir/foo.txt Exploring a module works well with the built-in Python help() and dir() functions. In the interpreter, do an "import os", and then use these commands look at what's available in the module: dir(os), help(os.listdir), dir(os.path), help(os.path.dirname). Running External Processes -- commandsThe *commands* module is a simple way to run an external command and capture its output.
## Given a dir path, run an external 'ls -l' on it -- ## shows how to call an external program def listdir(dir): cmd = 'ls -l ' + dir print "Command to run:", cmd ## good to debug cmd before actually running it (status, output) = commands.getstatusoutput(cmd) if status: ## Error case, print the command's output to stderr and exit sys.stderr.write(output) sys.exit(1) print output ## Otherwise do something with the command's output ExceptionsAn exception represents a run-time error that halts the normal execution at a particular line and transfers control to error handling code. This section just introduces the most basic uses of exceptions. For example a run-time error might be that a variable used in the program does not have a value (ValueError .. you've probably seen that one a few times), or a file open operation error because that a does not exist (IOError). (See [[https://docs.python.org/tut/node10.html][exception docs]]) Without any error handling code (as we have done thus far), a run-time exception just halts the program with an error message. That's a good default behavior, and you've seen it many times. You can add a "try/except" structure to your code to handle exceptions, like this: try: ## Either of these two lines could throw an IOError, say ## if the file does not exist or the read() encounters a low level error. f = open(filename, 'rU') text = f.read() f.close() except IOError: ## Control jumps directly to here if any of the above lines throws IOError. sys.stderr.write('problem reading:' + filename) ## In any case, the code then continues with the line after the try/except The try: section includes the code which might throw an exception. The except: section holds the code to run if there is an exception. If there is no exception, the except: section is skipped (that is, that code is for error handling only, not the "normal" case for the code). You can get a pointer to the exception object itself with syntax "except IOError, e: .. (e points to the exception object)". HTTP -- urllib and urlparseThe module *urllib* provides url fetching -- making a url look like a file you can read form. The *urlparse* module can take apart and put together urls.
## Given a url, try to retrieve it. If it's text/html, ## print its base url and its text. def wget(url): ufile = urllib.urlopen(url) ## get file-like object for url info = ufile.info() ## meta-info about the url content if info.gettype() == 'text/html': print 'base url:' + ufile.geturl() text = ufile.read() ## read all its text print text The above code works fine, but does not include error handling if a url does not work for some reason. Here's a version of the function which adds try/except logic to print an error message if the url operation fails. ## Version that uses try/except to print an error message if the ## urlopen() fails. def wget2(url): try: ufile = urllib.urlopen(url) if ufile.info().gettype() == 'text/html': print ufile.read() except IOError: print 'problem reading url:', url ExerciseTo practice the file system and external-commands material, see the Copy Special Exercise. To practice the urllib material, see the Log Puzzle Exercise. |
Leone Learning Systems, Inc. (LLS) is a North Shore company that provides online courses for kids anywhere and local teaching and tutoring services for students in Chicago and the Northern Suburbs of Chicagoland. LLS also provides a free geometry software package for children age 6 and up, and free resources for teachers and parents. This site includes information about classes taught, availability for tutoring, learning activities for kids, lesson plans, and ongoing software and curriculum research and development efforts. |