Python Tutorial - os.path Module Functions

Python Exercise Lists

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 ([' ', ' ']))
# Get directory
print (path.dirname ('C: //a.txt'))
# Determine if the specified directory exists
print (path.exists (a.txt'))

The output is:

C: //


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises