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

Collections in Python

Python collections are just containers which can contain data objects of various data-types inside them. Each collection...
28/12/2020

Python NumPy Tutorial

In this lesson on Python NumPy library, we will look at how this library allows us to manage powerful N-dimensional array...
29/12/2020

Python Virtualenv Tutorial

The headaches of dependency management are common to developers. One errant update requires hours of research to correct. ...
28/12/2020
Bài Viết

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

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

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024