Python os.path Module
Python OS File/Directory Methods
The os.path module is mainly used to obtain file attributes.
The following 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 shared by all paths in a list (multiple paths) |
| os.path.dirname(path) | Returns the file path |
| os.path.exists(path) | If the path exists, returns True; if the path does not exist or is broken, returns False. |
| os.path.lexists(path) | Returns True if the path exists, and also returns True if the path is broken |
| os.path.expanduser(path) | Convert the "~" contained in path~and~userinto the user directory |
| os.path.expandvars(path) | Replace according to the values of environment variablespathcontained in$nameand${name} |
| os.path.getatime(path) | Returns the last access time (floating-point seconds) |
| os.path.getmtime(path) | Returns the last file modification time |
| os.path.getctime(path) | Returns the creation time of file path |
| os.path.getsize(path) | Returns the file size, or returns an error if the file does not exist |
| os.path.isabs(path) | Determines whether it is an absolute path |
| os.path.isfile(path) | Determines whether the path is a file |
| os.path.isdir(path) | Determines whether the path is a directory |
| os.path.islink(path) | Determines whether the path is a link |
| os.path.ismount(path) | Determines whether the path is a mount point |
| os.path.join(path1[, path2[, ...]]) | Combines a directory and a file name into a path |
| os.path.normcase(path) | Converts the case and slashes of path |
| os.path.normpath(path) | Normalizes the path string form |
| os.path.realpath(path) | Returns the real path of path |
| os.path.relpath(path[, start]) | Calculates the relative path starting from start |
| os.path.samefile(path1, path2) | Determines whether directories or files are the same |
| os.path.sameopenfile(fp1, fp2) | Determines whether fp1 and fp2 point to the same file |
| os.path.samestat(stat1, stat2) | Determines whether stat tuples 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) | Generally used on Windows, returns a tuple of drive name and path |
| os.path.splitext(path) | Splits the path, returning a tuple of path name and file extension |
| os.path.splitunc(path) | Splits the path into mount point and file |
| os.path.walk(path, visit, arg) | Traverses path, calling the visit function for each directory entered. The visit function must have 3 parameters (arg, dirname, names); dirname represents the directory name of the current directory, names represents all file names under the current directory, and arg is the third parameter of walk. |
| os.path.supports_unicode_filenames | Sets whether Unicode path names are supported |
Example
The following example demonstrates the use of os.path related methods:
Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
print( os.path.basename('/root/example.txt') ) # Returns the file name
print( os.path.dirname('/root/example.txt') ) # Returns the directory path
print( os.path.split('/root/example.txt') ) # Splits file name and path
print( os.path.join('root','test','example.txt') ) # Combines a directory and file name into a path
Executing the above program outputs:
example.txt
/root
('/root', 'example.txt')
root/test/example.txt
The following example outputs file-related information.
Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import time
file='/root/example.txt' # File path
print( os.path.getatime(file) ) # Output last access time
print( os.path.getctime(file) ) # Output file creation time
print( os.path.getmtime(file) ) # Output last modification time
print( time.gmtime(os.path.getmtime(file)) ) # Output last modification time in struct_time form
print( os.path.getsize(file) ) # Output file size (in bytes)
print( os.path.abspath(file) ) # Output absolute path
print( os.path.normpath(file) ) # Normalize path string form
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/example.txt /root/example.txtOther extensions