Python3 os.path() Module
Python3 OS File/Directory Methods
The os.path module is primarily used for obtaining file attributes.
Here are several commonly used methods of the os.path module:
Method | Description |
---|---|
os.path.abspath(path) | Returns the absolute path |
os.path.basename(path) | Returns the file name |
os.path.commonprefix(list) | Returns the longest common path in the list of paths |
os.path.dirname(path) | Returns the file path |
os.path.exists(path) | Returns True if the path exists, False if it is broken |
os.path.lexists | Returns True if the path exists, True even if it is broken |
os.path.expanduser(path) | Converts "~" and "~user" in the path to the user directory |
os.path.expandvars(path) | Replaces "$name" and "${name}" in the path with the corresponding environment variable values |
os.path.getatime(path) | Returns the last access time (in seconds as a float) |
os.path.getmtime(path) | Returns the last file modification time |
os.path.getctime(path) | Returns the creation time of the file path |
os.path.getsize(path) | Returns the file size, or an error if the file does not exist |
os.path.isabs(path) | Checks if it is an absolute path |
os.path.isfile(path) | Checks if the path is a file |
os.path.isdir(path) | Checks if the path is a directory |
os.path.islink(path) | Checks if the path is a link |
os.path.ismount(path) | Checks if the path is a mount point |
os.path.join(path1[, path2[, ...]]) | Combines directory and file names into a single path |
os.path.normcase(path) | Converts the case and slashes in the path |
os.path.normpath(path) | Normalizes the path string |
os.path.realpath(path) | Returns the real path of the path |
os.path.relpath(path[, start]) | Calculates the relative path from start |
os.path.samefile(path1, path2) | Checks if the directories or files are the same |
os.path.sameopenfile(fp1, fp2) | Checks if fp1 and fp2 point to the same file |
os.path.samestat(stat1, stat2) | Checks if stat tuple stat1 and stat2 point to the same file |
os.path.split(path) | Splits the path into dirname and basename, returning a tuple |
os.path.splitdrive(path) | Typically used on Windows, returns a tuple of drive name and path |
os.path.splitext(path) | Splits the file name and extension in the path |
os.path.splitunc(path) | Splits the path into the mount point and file |
os.path.walk(path, visit, arg) | Traverses the path, calling the visit function for each directory, which must have 3 arguments (arg, dirname, names), where dirname is the current directory name and names are the file names in the current directory, and arg is the third argument of walk |
os.path.supports_unicode_filenames | Sets whether to support Unicode path names |
Example
The following example demonstrates the use of os.path methods:
#!/usr/bin/env python
import os.path
# Current file name
print(__file__)
# Absolute path of the current file
print(os.path.abspath(__file__))
# Path of the current file
print(os.path.dirname(os.path.abspath(__file__)))
The following example outputs information about the file:
test.py
/tutorialpro/tutorialpro-test-py/test.py
/tutorialpro/tutorialpro-test-py
#!/usr/bin/python3
import os
print(os.path.basename('/root/tutorialpro.txt')) # Returns the file name
print(os.path.dirname('/root/tutorialpro.txt')) # Returns the directory path
print(os.path.split('/root/tutorialpro.txt')) # Splits the file name and path
print(os.path.join('root', 'test', 'tutorialpro.txt')) # Combines directory and file name into a path
Executing the above program outputs:
tutorialpro.txt
/root
('/root', 'tutorialpro.txt')
root/test/tutorialpro.txt
The following example outputs file information.
Example
import os
import time
file = '/root/tutorialpro.txt' # File path
print(os.path.getatime(file)) # Outputs the most recent access time
print(os.path.getctime(file)) # Outputs the file creation time
print(os.path.getmtime(file)) # Outputs the most recent modification time
print(time.gmtime(os.path.getmtime(file))) # Outputs the most recent modification time in struct_time format
print(os.path.getsize(file)) # Outputs the file size in bytes
print(os.path.abspath(file)) # Outputs the absolute path
print(os.path.normpath(file)) # Normalizes the path string
Executing the above program outputs:
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/tutorialpro.txt
/root/tutorialpro.txt