Access 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.
os Module File/Directory Function | Operation |
---|---|
File Processing | |
remove()/unlink() | delete file |
rename() | rename file |
*stat()[a] | return file statistics |
symlink() | create symbolic link |
utime() | update timestamp |
Directories/Folders | |
chdir() | change working directory |
listdir() | list files in directory |
getcwd() | return current working directory |
mkdir()/makedirs() | create directory(ies) |
rmdir()/removedirs() | remove directory(ies) |
Access/Permissions (available only on Unix
![]() ![]() | |
access() | verify permission modes ![]() |
chmod() | change permission modes ![]() ![]() |
umask() | set default permission modes ![]() ![]() |
[a] includes stat(), lstat(), xstat()
os.path Pathname Function | Operation |
---|---|
Separation | |
basename() | remove directory path and return leaf name |
dirname() | remove leaf name and return directory path |
join() | join separate components into single pathname |
split() | return (dirname(), basename()) tuple |
splitdrive() | return (drivename, pathname) tuple |
splitext() | return (filename, extension) tuple |
Information | |
getatime() | return last file access time |
getmtime() | return last file modification time |
getsize() | return file size (in bytes) |
Inquiry | |
exists() | does pathname (file or directory) exist? |
isdir() | does pathname exist and is a directory? |
isfile() | does pathname exist and is a file? |
islink() | does pathname exist and is a symbolic link? |
samefile() | do both pathnames point to the same file? |
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. 1 #!/usr/bin/env python 2 3 import os 4 for tmpdir in ('/tmp', 'c:/windows/temp'): 5 if os.path.isdir(tmpdir): 6 break 7 else: 8 print 'no temp directory available' 9 tmpdir = '' 10 11 if tmpdir: 12 os.chdir(tmpdir) 13 cwd = os.getcwd() 14 print '*** current temporary directory' 15 print cwd 16 17 print '*** creating example directory…' 18 os.mkdir('example') 19 os.chdir('example') 20 cwd = os.getcwd() 21 print '*** new working directory:' 22 print cwd 23 print '*** original directory listing:' 24 print os.listdir(cwd) 25 26 print '*** creating test file…' 27 file = open('test', 'w') 28 file.write('foo ') 29 file.write('bar ') 30 file.close() 31 print '*** updated directory listing:' 32 print os.listdir(cwd) 33 34 print "*** renaming 'test' to 'filetest.txt'" 35 os.rename('test', 'filetest.txt') 36 print '*** updated directory listing:' 37 print os.listdir(cwd) 38 39 path = os.path.join(cwd, os.listdir (cwd)[0]) 40 print '*** full file pathname' 41 print path 42 print '*** (pathname, basename) ==' 43 print os.path.split(path) 44 print '*** (filename, extension) ==' 45 print os.path.splitext(os.path.basename (path)) 46 47 print '*** displaying file contents:' 48 file = open(path) 49 allLines = file.readlines() 50 file.close() 51 for eachLine in allLines: 52 print eachLine, 53 54 print '*** deleting test file' 55 os.remove(path) 56 print '*** updated directory listing:' 57 print os.listdir(cwd) 58 os.chdir(os.pardir) 59 print '*** deleting test directory' 60 os.rmdir('example') 61 print '*** DONE' |
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 emp *** creating example directory… *** new working directory: c:windows empexample *** 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 empexamplefiletest.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 ') >>> file.write('bar ') >>> 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.
CORE MODULE(S): os (and os.path)
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.