Compared to the pathlib module, the os.path module not only provides some methods for manipulating path strings, but also contains some methods for specifying file attributes, as shown in the following table:
Method | Description |
---|---|
os.path.abspath(path) | Returns the absolute path of path. |
os.path.basename(path) | Get the base name of the path, that is, the string between the end of path and the position of the last slash. |
os.path.commonprefix(list) | Returns the longest path common to all paths in a list (multiple paths). |
os.path.dirname(path) | Returns the directory portion of path. |
os.path.exists(path) | Determines whether the file corresponding to path exists. If it exists, it returns True; otherwise, it returns False. The difference from lexists() is that exists() will automatically determine broken file links (similar to file shortcuts in Windows systems), while lexists() will not. |
os.path.lexists(path) | Determines whether the path exists. If it exists, it returns True; otherwise, it returns False. |
os.path.expanduser(path) | Turn "~" and "~ user" contained in path into user directories. |
os.path.expandvars(path) | Replace "$ name" and "$ {name}" contained in path based on the value of the environment variable. |
os.path.getatime(path) | Returns the last access time (floating point seconds) of the file pointed to by path. |
os.path.getmtime(path) | Returns the last modification time of the file (in seconds). |
os.path.getctime(path) | Returns the creation time of the file in seconds since January 1, 1970 (also known as Unix time). |
os.path.getsize(path) | Returns the file size, or an error if the file does not exist. |
os.path.isabs(path) | Determines whether it is an absolute path. |
os.path.isfile(path) | Determine if the path is a file. |
os.path.isdir(path) | Determines whether the path is a directory. |
os.path.islink(path) | Determine if the path is a linked file (similar to a shortcut on a Windows system). |
os.path.ismount(path) | Determine whether the path is a mount point. |
os.path.join(path1[, path2[, ...]]) | Combine directories and file names into one path. |
os.path.normcase(path) | Convert case and slash to path. |
os.path.normpath(path) | Canonical path as a string. |
os.path.realpath(path) | Returns the true path of path. |
os.path.relpath(path[, start]) | Calculate relative paths from start. |
os.path.samefile(path1, path2) | Determine if the directories or files are the same. |
os.path.sameopenfile(fp1, fp2) | Determine if fp1 and fp2 point to the same file. |
os.path.samestat(stat1, stat2) | Determine if stat1 and stat2 point to the same file. |
os.path.split(path) | Split the path into dirname and basename and return a tuple. |
os.path.splitdrive(path) | Generally used in windows, returns a tuple of drive name and path. |
os.path.splitext(path) | Split path, return tuple of path name and file extension. |
os.path.splitunc(path) | Split the path into load points and files. |
os.path.walk(path, visit, arg) | Traverse the path, call the visit function into each directory, the visit function must have 3 parameters (arg, dirname, names), dirname represents the directory name of the current directory, names represents all file names in the current directory, args is the walk of the third parameter. |
os.path.supports_unicode_filenames | Sets whether any Unicode string can be used as the file name. |
The following program demonstrates the function and usage of some functions in Table 1:
from os import path # Get absolute path print (path.abspath ("a.txt")) # Get common prefix print (path.commonprefix (['C: //my_file.txt', 'C: //a.txt'])) # Get common path print (path.commonpath ([' http://www.freelearningpoints.com ', ' http://www.freelearningpoints.com/python/tutorials/python-pickle-module '])) # Get directory print (path.dirname ('C: //a.txt')) # Determine if the specified directory exists print (path.exists (a.txt'))
The output is:
C:\Users\lim.jouhui\a.txt
C: //
http:
C:
True
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises