Python SYS Module

28/12/2020

In this lesson on Python sys module, we will study how this module allows us to interact with the interpreter and the host machine directly. Let’s see what all features this module offers to us.

Python sys module

The first advantage the sys module offers to us is its independence from the host machine Operating System. This means that this module can work the same even if it is working on Windows or Macintosh or Linux or any given OS. In this post, we will study the functions it provides to us. Let’s get started.

Working with sys module

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

import sys

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

Python sys.argv

The most basic operation sys module allow us is to access the command line parameters passed to the script. Let’s write a program to demonstrate this:

import sys

print(‘Passed Command line parameters:’)
for param in sys.argv:
print(param)

When we run this program with command line parameters, we will see this output:

Note how this picked even the file name we passed to it as a command line parameter.

Python sys.path

Using the path function, we can display the PYTHONPATH which is provided in the host machine. We will write a script to use this function now:

import sys
print(n PYTHONPATH: ‘, sys.path)

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

Of course, this can vary based on your machine.

Python sys.stdin

We can use the sys module to take input from the user as well. We will try this function now:

import sys

input_by_user = sys.stdin.readline()
print("User Input : " + input_by_user)

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

This is very important function which we will usually use in our programs to take user input.

Python sys.copyright

To display the copyright information related to currently installed Python version on the host machine, we can use this function:

import sys
print(sys.copyright)

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

Python sys.exit

There are many use cases when we must choose to exit the program flow without doing anything else (like error handling etc.). This is easy to achieve using the exit function call:

import sys

print("Hello Linuxhint")
sys.exit(1)
print("Not printed!")

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

In this quick post on Python, we saw how we can use various functions provided by sys module in Python and demonstrated them with example scripts. Go on and play with more of them.

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

How to Calculate Matrices in Python Without NumPy

For many applications, you need mathematics. In Python there is the maths module which handles the basics such as rounding,...
29/12/2020

Logging Into Websites With Python

The login feature is an important functionality in today’s web applications. This feature helps keep special content...
29/12/2020

Using Python to Zip Files

With data increasing in size exponentially every day, your concern about how to keep the said data without over-flowing...
29/12/2020
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024