See All Titles |
![]() ![]() File SystemAccess to your file system occurs mostly through the Python os module. This module serves as the primary interface to your operating system facilities and services from Python. The os module is actually a front-end to the real module that is loaded, a module that is clearly operating system-dependent. This "real" module may be one of the following: posix (Unix), nt (Windows), mac (Macintosh), dos (DOS), os2 (OS/2), etc. You should never import those modules directly. Just import os and the appropriate module will be loaded, keeping all the underlying work hidden from sight. Depending on what your system supports, you may not have access to some of the attributes which may be available in other operating system modules. In addition to managing processes and the process execution environment, the os module performs most of the major file system operations that the application developer may wish to take advantage of. These features include removing and renaming files, traversing the directory tree, and managing file accessibility. Table 9.5 lists some of the more common file or directory operations available to you from the os module. A second module that performs specific pathname operations is also available. The os.path module is accessible through the os module. Included with this module are functions to manage and manipulate file pathname components, obtain file or directory information, and make file path inquiries. Table 9.6 outlines some of the more common functions in os.path. These two modules allow for consistent access to the file system regardless of platform or operating system. The program in Example 9.1 (ospathex.py) test drives some of these functions from the os and os.path modules.
Example 9.1. os & os.path Modules Example (ospathex.py)This code exercises some of the functionality found in the os and os.path modules. It creates a test file, populates a small amount of data in it, renames the file, and dumps its contents. Other auxiliary file operations are performed as well, mostly pertaining to directory tree traversal and file pathname manipulation. <$nopage> 001 1 #!/usr/bin/env python 002 2 003 3 import os 004 4 for tmpdir in ('/tmp', 'c:/windows/temp'): 005 5 if os.path.isdir(tmpdir): 006 6 break 007 7 else: <$nopage> 008 8 print 'no temp directory available' 009 9 tmpdir = '' 010 10 011 11 if tmpdir: 012 12 os.chdir(tmpdir) 013 13 cwd = os.getcwd() 014 14 print '*** current temporary directory' 015 15 print cwd 016 16 017 17 print '*** creating example directory…' 018 18 os.mkdir('example') 019 19 os.chdir('example') 020 20 cwd = os.getcwd() 021 21 print '*** new working directory:' 022 22 print cwd 023 23 print '*** original directory listing:' 024 24 print os.listdir(cwd) 025 25 026 26 print '*** creating test file…' 027 27 file = open('test', 'w') 028 28 file.write('foo\n') 029 29 file.write('bar\n') 030 30 file.close() 031 31 print '*** updated directory listing:' 032 32 print os.listdir(cwd) 033 33 034 34 print "*** renaming 'test' to 'filetest.txt'" 035 35 os.rename('test', 'filetest.txt') 036 36 print '*** updated directory listing:' 037 37 print os.listdir(cwd) 038 38 039 39 path = os.path.join(cwd, os.listdir (cwd)[0]) 040 40 print '*** full file pathname' 041 41 print path 042 42 print '*** (pathname, basename) ==' 043 43 print os.path.split(path) 044 44 print '*** (filename, extension) ==' 045 45 print os.path.splitext(os.path.basename (path)) 046 46 047 47 print '*** displaying file contents:' 048 48 file = open(path) 049 49 allLines = file.readlines() 050 50 file.close() 051 51 for eachLine in allLines: 052 52 print eachLine, 053 53 054 54 print '*** deleting test file' 055 55 os.remove(path) 056 56 print '*** updated directory listing:' 057 57 print os.listdir(cwd) 058 58 os.chdir(os.pardir) 059 59 print '*** deleting test directory' 060 60 os.rmdir('example') 061 61 print '*** DONE' 062 <$nopage> Running this program on a Unix platform, we get the following output: % ospathex.py *** current temporary directory /tmp *** creating example directory… *** new working directory: /tmp/example *** original directory listing: [] *** creating test file… *** updated directory listing: ['test'] *** renaming 'test' to 'filetest.txt' *** updated directory listing: ['filetest.txt'] *** full file pathname: /tmp/example/filetest.txt *** (pathname, basename) == ('/tmp/example', 'filetest.txt') *** (filename, extension) == ('filetest', '.txt') *** displaying file contents: foo bar *** deleting test file *** updated directory listing: [] *** deleting test directory *** DONE Running this example from a DOS window results in very similar execution: C:\>python ospathex.py *** current temporary directory c:\windows\temp *** creating example directory… *** new working directory: c:\windows\temp\example *** original directory listing: [] *** creating test file… *** updated directory listing: ['test'] *** renaming 'test' to 'filetest.txt' *** updated directory listing: ['filetest.txt'] *** full file pathname: c:\windows\temp\example\filetest.txt *** (pathname, basename) == ('c:\\windows\\temp\\example', 'filetest.txt') *** (filename, extension) == ('filetest', '.txt') *** displaying file contents: foo bar *** deleting test file *** updated directory listing: [] *** deleting test directory *** DONE Rather than providing a line-by-line explanation here, we will leave it to the reader as an exercise. However, we will walk through a similar interactive example (including errors) to give you a feel for what it is like to execute this script one step at a time. We will break into the code every now and then to describe the code we just encountered. >>> import os >>> os.path.isdir('/tmp') 1 >>> os.chdir('/tmp') >>> cwd = os.getcwd() >>> cwd '/tmp' This first block of code consists of importing the os module (which also grabs the os.path module). We verify that '/tmp' is a valid directory and change to that temporary directory to do our work. When we arrive, we call the getcwd() method to tell us where we are. >>> os.mkdir('example') >>> cwd = os.getcwd() >>> cwd '/tmp/example' >>> >>> os.listdir() Traceback (innermost last): File "<stdin>", line 1, in ? TypeError: function requires at least one argument >>> >>> os.listdir(cwd) [] Next, we create a subdirectory in our temporary directory, after which we will use the listdir() method to confirm that the directory is indeed empty (since we just created it). The problem with our first call to listdir() was that we forgot to give the name of the directory we want to list. That problem is quickly remedied on the next line of input. >>> file = open('test', 'w') >>> file.write('foo\n') >>> file.write('bar\n') >>> file.close() >>> os.listdir(cwd) ['test'] We then create a test file with two lines and verify that the file has been created by listing the directory again afterwards. >>> os.rename('test', 'filetest.txt') >>> os.listdir(cwd) ['filetest.txt'] >>> >>> path = os.path.join(cwd, os.listdir(cwd)[0]) >>> path '/tmp/example/filetest.txt' >>> >>> os.path.isfile(path) 1 >>> os.path.isdir(path) 0 >>> >>> os.path.split(path) ('/tmp/example', 'filetest.txt') >>> >>> os.path.splitext(os.path.basename(path)) ('filetest', '.ext') This section is no doubt an exercise of os.path functionality, testing join(), isfile(), isdir() which we have seen earlier, split(), basename(), and splitext(). We also call the rename() function from os. >>> file = open(path) >>> file.readlines() >>> file.close() >>> >>> for eachLine in allLines: … print eachLine, … foo bar This next piece of code should be familiar to the reader by now, since this is the third time around. We open the test file, read in all the lines, close the file, and display each line, one at a time. >>> os.remove(path) >>> os.listdir(cwd) [] >>> os.chdir(os.pardir) >>> os.rmdir('example') This last segment involves the deletion of the test file and test directory concluding execution. The call to chdir() moves us back up to the main temporary directory where we can remove the test directory (os.pardir contains the parent directory string ".." for Unix and Windows; the Macintosh uses "::"). It is not advisable to remove the directory that you are in. NOTE As you can tell from our lengthy discussion above, the os and os.path modules provide different ways to access the file system on your computer. Although our study in this chapter is restricted to file access only, the os module can do much more. It lets you manage your process environment, contains provisions for low-level file access, allows you to create and manage new processes, and even enables your running Python program to "talk" directly to another running program. You may find yourself a common user of this module in no time. Read more about the os module in Chapter 14.
|
© 2002, O'Reilly & Associates, Inc. |