The os Module
The os
module is an umbrella module that presents a reasonably uniform cross-platform view of the different capabilities of various operating systems. The module provides ways to create and handle files and directories, and to create, manage, and destroy processes. This section covers the filesystem-related capabilities of the os
module; Running Other Programs with the os Module covers the process-related capabilities.
The os
module supplies a name
attribute, which is a string that identifies the kind of platform on which Python is being run. Common values for name
are 'posix'
(all kinds of Unix-like platforms, including Mac OS X), 'nt'
(all kinds of 32-bit Windows platforms), 'mac'
(old Mac systems), and 'java'
(Jython). You can often exploit unique capabilities of a platform, at least in part, through functions supplied by os
. However, this book deals with cross-platform programming, not with platform-specific functionality, so I do not cover parts of os
that exist only on one kind of platform, nor do I cover platform-specific modules. All functionality covered in this book is available at least on both 'posix'
and 'nt'
platforms. However, I do cover any differences among the ways in which a given functionality is provided on different platforms.
OSError Exceptions
When a request to the operating system fails, os
raises an exception, which is an instance of OSError
. os
also exposes built-in exception class OSError
with the name os.error
. Instances of OSError
expose ...
Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.