Python OS Module provides the functions related path for different operating systems. The os.path module provides functions in order to use with string or byte parameters. In this tutorial, we will learn some popular OS Path module functions and usage which can be very helpful.
Return Path Base Name
A path consists of a directory name and base name which is the file name. The basename() method is used to return the base name of the given path. This generally means a file name or link name. The directories are removed from the path and returned as path base names.
import os
print(os.path.basename("/home/ismail/file.txt"))
print(os.path.basename("/etc/passwd"))
print(os.path.basename("/var/log/syslog"))
print(os.path.basename("/home/ismail/"))
print(os.path.basename("/home/log"))
If the path’s last character is / then the given path does not return a file because file names do not contain /. So the “/home/ismail/” does not contain a base name. The output is like below.
file.txt passwd syslog log
Return Directory Name
The directory name is the complete absolute path of the given path. The file name or base name part will be removed from the path. The dirname() method is used for this operation.
import os
print(os.path.dirname("/home/ismail/file.txt"))
print(os.path.dirname("/etc/passwd"))
print(os.path.dirname("/var/log/syslog"))
print(os.path.dirname("/home/ismail/"))
print(os.path.dirname("/home/log"))
The output is like below.
/home/ismail /etc /var/log /home/ismail /home
Check If Specified Path Is Absolute
A path can be specified in two ways which are called absolute or relative. The absolute path specifies the path complete from the root or drive letter. The isabs() method is used to check if the given path is absolute. As this is a check the return value is boolean True
or False
value. If the provided path is absolute the returned is boolean True and if not an absolute path return value is boolean False.
import os
print(os.path.isabs("/home/ismail/file.txt"))
print(os.path.isabs("/etc/passwd"))
print(os.path.isabs("log/syslog"))
print(os.path.isabs("/home/ismail/"))
print(os.path.isabs("./log"))
The output is like below.
True True False True False
Check If Specified Path Is Directory
The isdir() method is used to check if the specified directory exists and is a directory. If one of the checks fails the isdir() method returns False and if both of them are OK it will return True.
import os
print(os.path.isdir("/home/ismail/file.txt"))
print(os.path.isdir("/etc/passwd"))
print(os.path.isdir("log/syslog"))
print(os.path.isdir("/home/ismail/"))
print(os.path.isdir("/var/log"))
The output is like below.
False False False True True
Check If Specified Path Is File
The isfile() method is used to check if the specified path is a file and exists. If one of the checks fails the isfile() method returns False if both of the checks are OK it returns True.
import os
print(os.path.isfile("/home/ismail/file.txt"))
print(os.path.isfile("/etc/passwd"))
print(os.path.isfile("log/syslog"))
print(os.path.isfile("/home/ismail/"))
print(os.path.isfile("/var/log"))
The output is like below.
False True False False False
Normalize Case
The Linux and MacOSX use the path names as lowercase which is also lowercase too. But in Windows, the path names are converted into lowercase and forward slashes to backslashes. The windows type path names cases can be normalized with the normcase() method. but this method will work on Windows operating systems.
import os
print(os.path.normcase("/ETC/passwd"))
print(os.path.normcase("log/sYSLog"))
The output will be like the below.
\\etc\\passwd log\\syslog
Join Paths
The OS Path module provides the join() method which is used to join two or more paths together. This method can be very useful, especially with the directory name and base name concatenation.
import os
basename = "file.txt"
directoryname = "/home/ismail"
path = os.path.join(directoryname,basename)
print(path)
basename = "list.xls"
directoryname = "/home"
username="ismail"
path = os.path.join(directoryname,username,basename)
print(path)
The output will be like the below.
/home/ismail/file.txt /home/ismail/list.xls
Get Size Of Specified Path (File or Directory)
The specified path size can be returned with the getsize() method. This method can return the file or directory size. The directory size is 4096 bytes by default.
import os
print(os.path.getsize("/home/ismail"))
print(os.path.getsize("/var/log/syslog"))
The output will be like the below. The returned sizes are byte units.
4096 147570
Normalize Path
Paths can be expressed in different ways like parent or child etc. These types of path expressions are not absolute and are not displayed as the complete path. The os.path.normpath()
method can be used to normalize the path into the complete expression. The normalization made by according to the current working directory
import os
print(os.path.normpath("/home/../ismail"))
print(os.path.normpath("../ismail"))
print(os.path.normpath("./Downloads/.."))
/ismail ../ismail .
Common Path
If there are multiple paths and we need to find the most common path the os.path.commonpath()
method can be used. We provide multiple paths as list into the commonpath() method and it returns the common path for all of these paths.
import os
common_path=os.path.commonpath(["/var/log/test","/var/log/db/recent","/var/log/new"])
print(common_path)
/var/log