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 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.
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.
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 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 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 converted into the 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 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 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 below. The returned sizes are byte unit.
4096 147570