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

Virtual Environments in Python 3

Like most people, I hate installing unnecessary packages on my workstation. After you are done with them, uninstalling...
29/12/2020

Libvirt with Python

In one of my previous posts, I showed how one can get started with Libvirt and KVM. This virtualization stack is meant...
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
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024