OS Path Module¶
Indices and tables¶
Introduction¶
It is a VERY common requirement to manipulate and parse file system paths from within your application. In Python, there is no reason to write your own functions to do this as Python provides a very mature and robust set of functions for doing common path operations as part of the os.path
module.
The functions in os.path
accept paths as either unicode strings or bytes and return an object of the same type.
Tip
Also see the pathlib
module, which treats paths as class instances.
The examples that follow will provide a good overview of the functions in os.path
, but it will not show all of them.
Expanding Variables¶
The functions in os.path
don’t do any path expansion. Use os.path.expanduser()
and os.path.expandvars()
to explicitly make it happen. For example:
>>> os.path.expanduser('~/gitsandbox')
'/home/mike/gitsandbox'
>>> os.path.expandvars('~/gitsandbox/$LANGUAGE')
'~/gitsandbox/en_US'
Building Paths¶
>>> #
>>> # Obtain the absolute path of any path relative to the current
>>> # location in the filesystem.
>>> #
>>> os.path.abspath('./doc')
'/home/mike/gitsandbox/pythonedu-2017/doc'
>>> #
>>> # Obtain a relative path of any absolute path, in comparison
>>> # to the current location in the filesystem.
>>> #
>>> os.path.relpath('/home/mike/gitsandbox/pythonedu-2017/doc')
'doc'
>>> #
>>> # Join strings together to make a full path
>>> # :NOTE: The join method uses the correct path
>>> # seperator for the OS it is running on.
>>> #
>>> os.path.join("a", "b", "c")
a/b/c
Parsing Paths¶
>>> #
>>> # Extract the trailing entry from the path.
>>> #
>>> os.path.basename('/foo/bar/baz/bif')
'bif'
Note
If the path ends with a slash, then the last entry is not considered part of the basename. For example:
>>> os.path.basename('/foo/bar/baz/bif/')
''
>>> #
>>> # Extract the leading entries from the path.
>>> #
>>> os.path.dirname('/foo/bar/baz/bif')
'/foo/bar/baz'
Note
If the path ends with a slash, then the last entry is considered part of the dirname. For example:
>>> os.path.dirname('/foo/bar/baz/bif/')
'/foo/bar/baz/bif'
>>> #
>>> # Return a tuple of (dirname, basename).
>>> #
>>> os.path.split('/foo/bar/baz/bif')
('/foo/bar/baz', 'bif')
>>> #
>>> # Split the path into (drive, tail).
>>> # Alternatively, if a UNC path is given split it into
>>> # (ComputerName/SharedFolder, Resource).
>>> #
>>> os.path.splitdrive('c:/foo/bar/baz/bif')
('c:', '/foo/bar/baz/bif')
>>> os.path.splitdrive('//ComputerName/SharedFolder/Resource')
('//ComputerName/SharedFolder', '/Resource')
Note
The splitdrive()
function only works on Windows due to the fact that only Windows uses drive letters and UNC paths.
>>> #
>>> # Split the path so that the file extension is returned
>>> # seperate from the rest of the path, as in, (root, ext).
>>> #
>>> os.path.splitext('c:/foo/bar/baz/bif.py')
('c:/foo/bar/baz/bif', '.py')
Checking Paths¶
>>> #
>>> # Check if the path points to an existing path
>>> # or file descriptor.
>>> #
>>> os.path.exists('./pythonedu-2017/README.md')
True
>>> #
>>> # Return the time of last access of path.
>>> #
>>> os.path.getatime('./pythonedu-2017/README.md')
1521507412.0799189
>>> print(datetime.datetime.fromtimestamp(os.path.getatime('./pythonedu-2017/README.md')))
2018-03-19 20:56:52.079919
>>> #
>>> # Return the time of last modification of path.
>>> #
>>> os.path.getmtime('./pythonedu-2017/README.md')
1517362570.4536648
>>> print(datetime.datetime.fromtimestamp(os.path.getmtime('./pythonedu-2017/README.md')))
2018-01-30 20:36:10.453665
>>> #
>>> # Return the system’s ctime which, can be the creation
>>> # time (Windows) or the last time the metadata was
>>> # changed (Unix).
>>> #
>>> os.path.getctimes('./pythonedu-2017/README.md')
1521506812.5185046
>>> print(datetime.datetime.fromtimestamp(os.path.getctime('./pythonedu-2017/README.md')))
2018-03-19 20:46:52.518505
>>> #
>>> # Return the size, in bytes, of path.
>>> #
>>> os.path.getsize('./pythonedu-2017/README.md')
561
>>> #
>>> # Return True if path is an absolute pathname.
>>> #
>>> os.path.isabs('./pythonedu-2017/README.md')
False
>>> #
>>> # Return True if path is an existing regular file.
>>> #
>>> os.path.isfile('./pythonedu-2017/README.md')
True
>>> #
>>> # Return True if path is an existing directory.
>>> #
>>> os.path.isdir('./pythonedu-2017/README.md')
False
>>> #
>>> # Return True if path refers to a directory entry that is a symbolic link.
>>> #
>>> os.path.islink('./pythonedu-2017/README.md')
False
>>> #
>>> # Return True if pathname path is a mount point.
>>> #
>>> os.path.ismount('/home')
True
>>> os.path.ismount('/usr')
False