| An Introduction to Python by Guido van Rossum and Fred L. Drake, Jr. Paperback (6"x9"), 124 pages ISBN 0954161769 RRP £12.95 ($19.95) Sales of this book support the Python Software Foundation! Get a printed copy>>> |
10.1 Operating System Interface
The ‘os’ module provides dozens of functions for interacting with the operating system:
>>> import os
>>> os.system('time 0:02')
0
>>> os.getcwd() # Return the current working directory
'C:\\Python25'
>>> os.chdir('/server/accesslogs')
Be sure to use the ‘import os’ style instead of
‘from os import *’. This will keep os.open() from
shadowing the builtin open() function which operates much
differently.
The builtin dir() and help() functions are useful
as interactive aids for working with large modules like ‘os’:
>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the
module's docstrings>
For daily file and directory management tasks, the ‘shutil’ module provides a higher level interface that is easier to use:
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
>>> shutil.move('/build/executables', 'installdir')
| ISBN 0954161769 | An Introduction to Python | See the print edition |