Python provides a module named os which lets us access some of the functionalities of the underlying operating system and let us perform those functions from python. It let us performs functions like checking directories, creating directories, retrieve process details, modify environment variables, and many more. As a part of this tutorial, we'll be exploring some of the very commonly used and very often needed functions of os module. We have divided the tutorial into three sections where each section covers a bunch of os functions.
We'll start by importing os module.
import os
As a part of this section, we'll explore functions that will be useful very often like getting the current directory, changing the working directory, getting CPU cores, accessing/modifying environment variables, etc.
The getcwd() function returns current working directory. We can use it any time to get the current working path.
os.getcwd()
The chdir() function accepts the string path and changes the current working directory to that path. Below we have changed the working directory to explain the usage of the function.
print(os.getcwd())
os.chdir("/home/sunny/Desktop")
print(os.getcwd())
The os module provides us with environ attribute which lets us access and modifies environment variables. It also provides getenv() method which accepts string environment variable and returns the value of it.
Below we have retrieved few environment variables using environ attribute and getenv() method for explanation purposes.
print("Home Directory : {}".format(os.getenv("HOME")))
print("User Name : {}".format(os.getenv("USER")))
print("Desktop Session : {}".format(os.getenv("DESKTOP_SESSION")))
print("Language : {}".format(os.getenv("LANG")))
print("Home Directory : {}".format(os.environ["HOME"]))
print("User Name : {}".format(os.environ["USER"]))
print("Desktop Session : {}".format(os.environ["DESKTOP_SESSION"]))
print("Language : {}".format(os.environ["LANG"]))
We can modify existing environment variables as well as add new environment variables using environ attribute of os module. Below we have created a new environment variable and modify its value.
os.environ["New_ENV_VAR"] = "CoderzColumn"
print("User Environment Variable : {}".format(os.getenv("New_ENV_VAR")))
print("User Environment Variable : {}".format(os.environ["New_ENV_VAR"]))
The os module provides with a function named uname() which returns information about the underlying operating system as a tuple of 5 strings.
We can also access the underlying operating system name using name attribute of os module. It returns posix for unix and related systems, nt for windows, and java for java based systems.
sys_name, node_name, release, version, machine = os.uname()
print("System Name : {}".format(sys_name))
print("Node Name : {}".format(node_name))
print("Release : {}".format(release))
print("Version : {}".format(version))
print("Machine : {}".format(machine))
print("OS Name : {}".format(os.name))
We can list down all members in any given path using listdir() function. It returns a list of strings where each member is a member of the directory passed as a parameter to it. The member can be a file, directory, symbolic links, etc.
If we don't provide any path to the method then it retrieves all members of the current working directory.
os.listdir("/home/sunny/blogs")
The os module provides us with method named scandir() which lets us scan any directory to get information about its members. It takes as the input directory name and returns an iterator of DirEntry instances. The DirEntry instance holds detail about members like file name, full path, file type, file stat, etc.
If we don't provide any path to the method then it'll return an iterator for the current directory. Below we have listed down the contents of the blogs directory.
for dr in os.scandir("/home/sunny/blogs"):
print("Name : {}".format(dr.name))
print("Is File : {}".format(dr.is_file()))
print("Is Directory : {}".format(dr.is_dir()))
print("Is Symbolic Link : {}".format(dr.is_symlink()))
print("Full Path : {}\n".format(dr.path))
One of the most useful methods provided by os module for examining the contents of the path is walk() method. It takes as input any path and returns a list of tuples where each tuple has 3 entries.
If goes down recursively through all subdirectories of given input path.
for directory, sub_directories, files in os.walk("/home/sunny/blogs/Data Science"):
print("Directory Name : {}".format(directory))
print("Sub-directories :")
for sub_dir in sub_directories:
print("\t", sub_dir)
print("Files : ")
for file in files:
print("\t",file)
print()
for directory, sub_directories, files in os.walk("/home/sunny/blogs/Data Science", topdown=False):
print("Directory Name : {}".format(directory))
print("Sub-directories :")
for sub_dir in sub_directories:
print("\t", sub_dir)
print("Files : ")
for file in files:
print("\t",file)
print()
The os module let us execute the underlying operating system command using system() method. It takes as input string containing the command and executes it.
Below we have executed Linux ls commands which list down the contents of a given directory.
os.system("ls /home/sunny/blogs")
Android 'In Progress' Python
'Artificial Intelligence' LICENSE README.md
'Data Science' 'Machine Learning' terms-and-conditions.ipynb
'Digital Marketing' privacy-policy.ipynb
os.system("ls -l /home/sunny/blogs")
drwxr-xr-x 2 sunny sunny 4096 Feb 17 2020 Android
drwxr-xr-x 3 sunny sunny 4096 Dec 8 2020 'Artificial Intelligence'
drwxr-xr-x 3 sunny sunny 4096 Feb 6 2020 'Data Science'
drwxr-xr-x 3 sunny sunny 4096 Aug 4 2020 'Digital Marketing'
drwxr-xr-x 2 sunny sunny 4096 Apr 23 2020 'In Progress'
-rw-r--r-- 1 sunny sunny 1070 Dec 30 2019 LICENSE
drwxr-xr-x 3 sunny sunny 4096 Jan 1 2020 'Machine Learning'
-rw-r--r-- 1 sunny sunny 4215 Feb 14 2020 privacy-policy.ipynb
drwxr-xr-x 3 sunny sunny 4096 Feb 25 2020 Python
-rw-r--r-- 1 sunny sunny 31 Dec 30 2019 README.md
-rw-r--r-- 1 sunny sunny 2685 Jan 1 2020 terms-and-conditions.ipynb
Below we have executed ls command and given the output of it to grep command which only keeps entries with string .ipynb present in them. The grep command is used to capture lines with a particular string in them.
os.system("ls -l /home/sunny/blogs | grep .ipynb")
-rw-r--r-- 1 sunny sunny 4215 Feb 14 2020 privacy-policy.ipynb
-rw-r--r-- 1 sunny sunny 2685 Jan 1 2020 terms-and-conditions.ipynb
We can retrieve a number of CPU/cores on any computer by using cpu_count() command of os module.
os.cpu_count()
If you want random bytes of a particular size then urandom() command can be very useful. It takes as input integer specifying the number of bytes and returns that many random bytes. These can be useful in many cryptographic algorithms.
os.urandom(10)
We can retrieve terminal size by using get_terminal_size() command. It returns us the terminal size of a number of columns and number of lines.
os.get_terminal_size()
We can retrieve current logged in user name by using getlogin() function of os module.
os.getlogin()
If we want to know which directories will be searched when we try to execute any executable in the current operating system then we can use get_exec_path() function. It can be useful when we are facing an issue with some executables. It's better to check whether the path where executable is installed is getting checked or not.
os.get_exec_path()
As a part of this section, we'll work with functions that can be helpful in directories and files related functionalities like creating directories, removing directories, retrieving directory stats, etc.
We can create a directory using mkdir() function. We need to give is an absolute or relative path. It accepts a string and creates a directory from it. It requires that in order to create a directory in a particular path, all parent directories are already created else it'll fail.
print("Current Directory : {}".format(os.getcwd()))
os.mkdir("temp")
#os.mkdir("/home/sunny/Desktop/temp")
print("Is temp folder created? : {}".format("temp" in os.listdir(os.getcwd())))
Sometimes we need to recursively create directories. In this kind of situation mkdir() function will fail. We can use makedirs() function for these purposes. It recursively creates all directories until the last directory.
Below we can notice that when we try to create directory dir3 using mkdir() function, it fails because the parent directories dir1 and dir2 are not created yet. The makedirs() method solves this problem.
print("Current Directory : {}".format(os.getcwd()))
os.mkdir("temp/dir1/dir2/dir3")
print("Current Directory : {}".format(os.getcwd()))
os.makedirs("temp/dir1/dir2/dir3")
print("Contents of temp Folder : {}".format(os.listdir("temp")))
print("Contents of dir1 Folder : {}".format(os.listdir("temp/dir1")))
print("Contents of dir2 Folder : {}".format(os.listdir("temp/dir1/dir2")))
We can remove any particular directory by using rmdir() method. It takes as the input directory name and removes it. Below we have explained the example.
os.rmdir("temp/dir1/dir2/dir3")
print("Contents of temp Folder : {}".format(os.listdir("temp")))
print("Contents of dir1 Folder : {}".format(os.listdir("temp/dir1")))
print("Contents of dir2 Folder : {}".format(os.listdir("temp/dir1/dir2")))
The removedirs() function can be used to remove the directory and all its parent directories given in a path. Above, we deleted dir3 using rmdir() function which only deletes the given directory and not its parents.
Below we can notice that removedirs() deletes temp, dir1 and dir2 directories.
os.removedirs("temp/dir1/dir2")
print("Does temp folder exist? : {}".format("temp" in os.listdir(os.getcwd())))
We can rename directory by using rename() function. It takes as input two strings where the first string is the current name of the directory and the second input is the new name of the directory. We can also rename file names using this function.
os.rename("temp/dir1", "temp/dir11")
print("Contents of temp Folder : {}".format(os.listdir("temp")))
print("Contents of temp Folder Before Modification : {}".format(os.listdir("temp")))
os.rename("temp/temp.txt", "temp/temp_modified.txt")
print("Contents of temp Folder After Modification : {}".format(os.listdir("temp")))
We can retrieve the stats of any file by using stat() function. It returns stat_result object which has a list of details.
stat_result = os.stat("temp/temp_modified.txt")
print("File Mode Bits : {}".format(stat_result.st_mode))
print("Inode Number : {}".format(stat_result.st_ino))
print("Device Identifier : {}".format(stat_result.st_dev))
print("Number of Hard Links : {}".format(stat_result.st_nlink))
print("User Identifier : {}".format(stat_result.st_uid))
print("Group Identifier : {}".format(stat_result.st_gid))
print("File Size in Bytes : {}".format(stat_result.st_size))
import datetime
print("\nRecent Access Time : {}".format(datetime.datetime.fromtimestamp(stat_result.st_atime)))
print("Recent Content Modification Time : {}".format(datetime.datetime.fromtimestamp(stat_result.st_mtime)))
print("Recent Metadata Change Time : {}".format(datetime.datetime.fromtimestamp(stat_result.st_ctime)))
As a part of this section, we'll explain different methods which can help us retrieve information about the current process as well as other processes based on the process id.
We can retrieve current process id using getpid() and getuid() methods. The getuid() method returns current process's real user id.
The getgid() method returns current process's group id. We can retrieve parent process details using getppid() and getpgrp() methods.
The getgroups() method returns a list of additional groups to which the process belongs.
print("Current Process ID : {}".format(os.getpid()))
print("Current Process ID : {}".format(os.getuid()))
print("Current Group ID : {}".format(os.getgid()))
print("Current Process Group ID : {}".format(os.getpgrp()))
print("Parent Process ID : {}".format(os.getppid()))
print("Current Process Effective ID : {}".format(os.geteuid()))
print("List of Groups Process Belongs : {}".format(os.getgroups()))
We can retrieve the process group id and session id based on the process id as well. The getpgid() and getsid() methods take as an input process id and returns group id and session id of the process represented by that process id.
current_id = os.getpid()
print("Group ID of Process {} is {}".format(current_id, os.getpgid(current_id)))
print("Session ID of Process {} is {}".format(current_id, os.getsid(current_id)))
This ends our small tutorial explaining how to use a few very commonly needed functions of os module. Please feel free to let us know your views in the comments section.
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to