Open In App

Uses of OS and Sys in Python

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see where we use Os and Sys in Python with the help of code examples.

What is Os Module?

Python OS module in Python furnishes a versatile means of engaging with the operating system. It facilitates a range of operations, including the creation, deletion, renaming, movement, and modification of attributes for files and directories. Moreover, the module enables the execution of commands and programs, as well as the management of processes and environment variables. For using the Os in Python it imports first in your code.

Syntax:

import os

What is Sys Module?

The sys module in Python grants access to system-specific parameters and functionalities. It encompasses features such as standard input, output, and error streams, command-line arguments, exit status, interpreter version, platform name, executable path, module paths, maximum recursion depth, memory allocation limit, and more. For using the Sys in Python it imports first in your code.

Syntax:

import Sys

OsS and Sys Usage in Python

Below are some of the example by which we can understand about where we can use Os and Sys in Python:

Use of Os in Python

Example 1: Create New Directories

In this example, below code demonstrates directory manipulation using the os module. It first creates a new directory named “example_directory” using os.mkdir. Then, it changes the current working directory to this newly created directory with os.chdir. Finally, it lists the files in the current directory using os.listdir

Python3
import os

# Create a new directory
os.mkdir("example_directory")

# Change the current working directory
os.chdir("example_directory")

# List files in the current directory
files = os.listdir()
print("Files in the current directory:", files)

Output:

Files in the current directory: []

Example 2: File Path Manipulation

In this code, the `os` module is used to perform path manipulation. The `os.path.join` method combines the provided path elements (“path”, “to”, “file.txt”) to create a file path. The `os.path.abspath` method then retrieves the absolute path of the specified file (“file.txt”), and the result is printed as “Absolute path:”.

Python3
import os

# Join paths to create a file path
file_path = os.path.join("path", "to", "file.txt")

# Get the absolute path of a file
absolute_path = os.path.abspath("file.txt")
print("Absolute path:", absolute_path)

Output:

Absolute path: C:\Users\GFG0371\OneDrive\Pictures\Article Review\ppp\file.txt

Example 3: Environment Variables

In this example, below code uses the `os` module to retrieve the value of the “USERNAME” environment variable, typically representing the current user’s username. It then prints the obtained username.

Python3
import os

# Get the value of an environment variable
username = os.getenv("USERNAME")
print("Username:", username)

Output:

Username: GFG0371

Use of Sys in Python

Example 1: Command Line Arguments

In this example, below code uses the `sys` module to access command-line arguments. It retrieves the script name (`sys.argv[0]`) and a list of additional arguments (`sys.argv[1:]`), then prints both pieces of information, displaying the script name and the provided command-line arguments.

Python3
import sys

# Access command line arguments
script_name = sys.argv[0]
arguments = sys.argv[1:]

print("Script Name:", script_name)
print("Arguments:", arguments)

Output

Script Name: /usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py
Arguments: ['-f', '/root/.local/share/jupyter/runtime/kernel-98095183-6614-40b5-9c94-329e4204d252.json']

Example 2: Exiting the Program

In this example, below code checks if a command-line argument (input file) is provided. If not, it prints a usage message and exits the program with an error code. If an input file is provided, it continues to process the file, displaying a message indicating the ongoing file processing.

Python3
import sys

# Exit the program with an error message
if len(sys.argv) < 2:
    print("Usage: python script.py <input_file>")
    sys.exit(1)

input_file = sys.argv[1]
print("Processing file:", input_file)

Output

Processing file: -f

Example 3: System Version Information

In this example, below code uses the sys module to print the current Python interpreter’s version and version information, providing details about the Python runtime environment, including major, minor, and micro version numbers.

Python3
import sys

# Display Python version information
print("Python Version:", sys.version)
print("Python Version Info:", sys.version_info)

Output

Python Version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Python Version Info: sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads