Python OS Module

28/12/2020
In this lesson on the Python ‘OS’ module, we will study how this module allows us to perform common OS related operations. The first advantage of the OS module, is its independence from the host machine Operating System. This means that this module can work the same on any Linux distribution or even other operating systems. In this post, we will study the functions it provides to us. Let’s get started.

Working with OS module

To start working with the OS module and make scripts use this module, we will use the same import statement in all the scripts we write:

import os

This statement imports and brings required OS module dependencies into our scope.

os.name

This is the most basic operation we can perform with this module. This is self-descriptive in nature that this function will give the OS we are using right now:

import os
print(os.name)

When we run this program, we will see this output:

Of course, this script will give different output based on the host platforms.

os.environ

Using the environ process parameter, we can get data about the environment variables defined in the system. Let’s put this to use here:

import os
home_env = os.environ[‘HOME’]
print(home_env)

When we run this program, we will see this output:

Again, this script will give different output based on the configured params.

os.execvp

Using OS module, we can even execute other scripts present on the machine. For this, let’s define a sample script here, with name ‘sample.py’ and with following contents:

print("Hello LinuxHint");

In the program, let’s execute this script using the python interpreter:

import os

interpreter = "python"
script = ["hello.py"]

print(os.execvp(interpreter, (interpreter,) + tuple(script)))

When we run this program, we will see this output:

This is actually a very important command with which we can write scripts which run other scripts as well on the basis of the flows and conditions.

os.getuid

Using the getuid function, we can obtain the currents process ID (or PID). With this, we can control process as well. Let’s put this function to use:

import os
print(os.getuid())

When we run this program, we will see this output:

os.uname

Using the uname function, we can identify the current OS in detail. Let’s put this function to use:

import os
print(os.uname())

When we run this program, we will see this output:

This is quite the information regarding the platform.

os.listdir

Many times in our scripts, we can even obtain all available directories in current path of execution to perform any number of operations on them. We will only list available directories in a script here:

import os
print(os.listdir("."))

We provided a . here so that the script prints directories and files present in current directory. When we run this program, we will see this output:

os.system

Using os system function, we can run a command in the Python script, which will act as if we were running it directly from the command line. For example:

import os

files = os.system("users > users.txt")

When we run this program, we will see this output:

In this lesson, we read about various functions provided by the Python OS module. See more lessons on Python here.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install nornir Python Library on Ubnutu

Nornir is a Python library for automating network connected devices. You can compare it to Ansible, which is mainly used...
29/12/2020

Use python to zip a file and directory

A compressed file contains many files, directory and subdirectories. Many applications are available to create a compress...
29/12/2020

Top 10 Python Web Frameworks

When you want to make a website, you need HTML, JavaScript and CSS. To create a website using Python, you need a few other...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025