Python3 os.path() Module
Python3 OS File/Directory Methods
os.pathThe module is part of the Python standard libraryospart of the module, specifically used for operating and processing file paths.
os.pathIt provides a powerful set of tools for performing various operations on file and directory paths, such as getting file names, determining whether a path exists, path concatenation, path normalization, etc.
os.pathThe module performs well on cross-platform operating systems, allowing the same piece of code to handle path-related issues when running on different operating systems (such as Windows, Linux, macOS).
The following are several commonly used methods of the os.path module:
Path Operations
| Method | Description |
|---|---|
os.path.abspath(path) |
Convert a relative path to an absolute path. |
os.path.basename(path) |
Get the last part of the path, i.e., the file name. |
os.path.dirname(path) |
Get the directory part of the path. |
os.path.join(*paths) |
Join multiple paths, automatically handling path separators. |
os.path.split(path) |
Split the path into a tuple of directory and file name. |
os.path.splitext(path) |
Split the path into a tuple of file name and extension. |
Getting Path Information
| Method | Description |
|---|---|
os.path.exists(path) |
Determine whether the path exists. |
os.path.isfile(path) |
Determine whether the path is a file. |
os.path.isdir(path) |
Determine whether the path is a directory. |
os.path.getsize(path) |
Get the size of the file, in bytes. |
os.path.getatime(path) |
Get the last access time of the file. |
os.path.getmtime(path) |
Get the last modification time of the file. |
os.path.getctime(path) |
Get the creation time of the file (on some operating systems, this represents the last status change time). |
Path Normalization
| Method | Description |
|---|---|
os.path.normpath(path) |
Normalize the path, eliminating redundant separators and relative path markers. |
os.path.realpath(path) |
Get the real path of the file, resolving symbolic links. |
os.path.relpath(path, start=os.curdir) |
Calculate the relative path, fromstarttopath。 |
Path Comparison
| Method | Description |
|---|---|
os.path.commonpath(paths) |
Return the common path in a sequence of paths. |
os.path.commonprefix(list) |
Return the longest common prefix in a sequence of paths. |
os.path.samefile(path1, path2) |
Determine whether two paths point to the same file. |
os.path.sameopenfile(fp1, fp2) |
Determine whether two opened file objects point to the same file. |
os.path.samestat(stat1, stat2) |
Determine whether two files have the samestatstatus. |
Platform-Dependent Functions
| Method | Description |
|---|---|
os.path.splitdrive(path) |
On Windows, return a tuple of the drive/device part and the path part of the path. |
os.path.splitunc(path) |
Split the shared device and path parts in the path (Windows-specific). |
Example
The following example demonstrates the use of os.path-related methods:
Example
import os.path
# current file name
print(__file__)
# absolute path of the current file name
print( os.path.abspath(__file__) )
# returns the path of the current file
print(os.path.dirname( os.path.abspath(__file__) ))
The following example outputs file-related information.
test.py /example/example-test-py/test.py /example/example-test-py
Example
The output of running the above program is:
example.txt
/root
('/root', 'example.txt')
root/test/example.txt
The following example outputs file-related information.
Example
The output of running the above program is:
1539052805.5735736 1539052805.5775735 1539052805.5735736 time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=2, tm_min=40, tm_sec=5, tm_wday=1, tm_yday=282, tm_isdst=0) 7 /root/example.txt /root/example.txtOther Extensions