Cyberithub

11 Best Python OS Module Examples on Linux

In this tutorial, I will take you through 11 Best Python OS Module Examples on Linux. According to Python official Documentation os modules provides a portable way of using operating system dependent functionality. One can use these os modules to perform different OS functions. For example: os.chdir() can be used to change the directory, os.rmdir() method can be used to delete the directory, os.listdir() can be used to list out the directory files and so on. Here, we will look into few of the important python os modules and will try to understand their usages with the help of examples.

11 Best Python OS Module Examples on Linux 1

Python OS Module Examples on Linux

Also Read: Primitive Data Types in Java – int, char, byte, short, long, float, double and boolean

Example 1: Using os.name function

If you want to check if the server is posix compliant system then you can use python os.name as shown below. This will check if certain os specific modules are supported. Currently it registers following names in Python 2.7 – posix, nt, os2, ce, java and riscos whereas in Python 3.4, it only registers posix, nt and java.

Syntax

os.name

Parameters

No parameter required

Return Value

This function will return the os dependent module like posix, nt, os2, ce etc.

Program

[root@localhost python]# vi example.py
import os
print('OS Module is:' + os.name)

Run Your Script

[root@localhost python]# python example.py
OS Module is:posix

Example 2: Using os.getcwd() method

Every process running in a Server is associated with a working directory. If you want to check the current working directory of that process then you need to use python os.getcwd() method as described in below example.

Syntax

os.getcwd

Parameters

No parameters required

Return Value

This will return the directory where program is currently running.

Program 

[root@localhost python]# vi example.py
import os
print('OS Name is:' + os.getcwd())

Run Your Script

[root@localhost python]# python example.py
OS Name is:/root/python

Example 3: Using os.rmdir() method

If you want to remove an empty directory from a Server then you need to use os.rmdir() method as shown below. If the directory path you are trying to delete is not empty, then it will raise OSError.

Syntax

os.rmdir(path)

Parameters

path : this is the path of the directory which needs to be deleted

Return Value

This method does not return any value.

Program Example

[root@localhost python]# vi example.py
import os
final = "test1"
initial = "/root"
path = os.path.join(initial,final)
os.rmdir(path)
print("Deleted Directory Successfully")

Run Your Script

[root@localhost python]# python example.py
Deleted Directory Successfully

Example 4: Using os.mkdir() method

If you want to create a directory then you can use os.mkdir() method as shown in below example. If the directory is already available then it will raise FileExistsError. As you can see from below example, we are not specifying any mode so by default it will create directory with 777 permission.

Syntax

os.mkdir(path[,mode])

Parameters

path : this is the path of the directory which needs to be created

mode : this is the permission of the directory. If not specified, by default it will assign 777 permission.

Return Value

This method does not return any value.

Program Example

[root@localhost python]# vi example.py
import os
final = "test1"
initial = "/root"
path = os.path.join(initial,final)
os.mkdir(path)
print("Directory created Successfully")

Run Your Program

[root@localhost python]# python example.py
Directory created Successfully
[root@localhost python]# ls -lrtd /root/test1
drwxr-xr-x 2 root root 6 Oct 14 14:48 /root/test1

Example 5: Using os.getlogin() method

If you want to get the user which is controlling the current program, then you need to use os.getlogin() method as shown in the below example.

Syntax

os.getlogin()

Parameters

No parameters required.

Return Value

This returns the user name through which current program is running.

Program Example

[root@localhost python]# vi example.py
import os
path = os.getlogin()
print("Current Logged in User is: " + os.getlogin())

Run Your Program

[root@localhost python]# python example.py
Current Logged in User is: root

Example 6: Using os.chdir() method

If you want to change your current working directory then you need to use os.chdir() method as shown in below example. In this example, we are changing our current directory to /root/example directory path.

Syntax

os.chdir(path)

Parameters

path : this is the new given directory path

Return Value

This method does not return any value.

Program Example

[root@localhost python]# vi example.py
import os
path = "/root/example"
print("Current working directory is: " + os.getcwd())
os.chdir(path)
print("Changed Directory Path is: " + os.getcwd())

Run Your Program

[root@localhost python]# python example.py
Current working directory is: /root/python
Changed Directory Path is: /root/example

Example 7: Using os.listdir() method

If you want to list all the files in a directory path then you can use os.listdir() method as shown in below example. In this we are listing all the files present under /root/example directory.

Syntax
os.listdir(path)

Parameters

path : this is the directory path from where all the contents will be listed.

Return Value

This method returns list of files and directories.

Program

[root@localhost python]# vi example.py
import os,sys
path = "/root/example"
dirs = os.listdir(path)
for i in dirs:
print i

Run Your Program

[root@localhost python]# python example.py
hello.rs
hello
file

Example 8: Using os.getpid() method

If you want to check the process ID of the current running process then you need to use os.getpid() method as shown in below example.

Syntax

os.getpid()

Parameters

This does not require any parameter.

Return Value

This method will return the Process ID of current program.

Program

[root@localhost python]# vi example.py
import os
pid = os.getpid()
print(pid)

Run Your Program

[root@localhost python]# python example.py
3179

Example 9: Using os.getppid() method

If you want to get the parent process Id of the current process then you need to use os.getppid() method as shown in the below example.

Syntax

os.getppid()

Parameters

No parameters required

Return Value

This method will return parent process ID of the current running program.

Program

[root@localhost python]# vi example.py
import os
pid = os.getppid()
print(pid)

Run Your Program

[root@localhost python]# python example.py
1948

Example 10: Using os.setgid() method

If you want to set the group Id of the current running process then you need to use os.setgid() method as shown in the below example. In this example, we are setting group Id to 700 to the current running process.

Syntax

os.setgid(gid)

Parameters

gid : this is the Group ID value which needs to pass to the setgid() method.

Return Value

This method does not return any value

Program

[root@localhost python]# vi example.py
import os
print("Real Group ID:",os.getgid())
gid=700
os.setgid(gid)
print("Changed Group ID:",os.getgid())

Run Your Program

[root@localhost python]# python example.py
('Real Group ID:', 0)
('Changed Group ID:', 700)

Example 11: Using os.setuid() method

If you want to set the real user id then you need to use os.setuid() method as shown in the below example. In this example, we are setting the real user id to 700.

Syntax

os.setuid(uid)

Parameters

uid : this is the real user id which needs to be passed to setuid() method.

Return Value

This method does not return any value.

Program

[root@localhost python]# vi example.py
import os
print("Real User ID:",os.getuid())
gid=700
os.setuid(gid)
print("Changed User ID:",os.getuid())

Run Your Program

[root@localhost python]# python example.py
('Real User ID:', 0)
('Changed User ID:', 700)

 

 

 

 

 

Popular Recommendations:-

Step by Step Guide to Install Apache 2.4.6 Web Server on RHEL/CentOS 7

How to Install MariaDB 5.5 Server on RHEL/CentOS 7 Linux with Easy Steps

6 Simple Steps to Change/Reset MariaDB root password on RHEL/CentOS 7/8

Best Steps to Install Java on RHEL 8/CentOS 8

5 Examples to Turn Off SELinux Temporarily or Permanently on RHEL 8/CentOS 8

Best Explanation of Wrapper Classes in Java: Autoboxing and Unboxing with Examples

5 Best Ways to Become root user or Superuser in Linux (RHEL/CentOS/Ubuntu)

How to Install PHP on RedHat/CentOS 7 with Easy Steps

Leave a Comment