Unit VII
Unit VII
2
Sys Module
• The sys module in Python provides various functions and
variables that are used to manipulate different parts of the
Python runtime environment.
• It allows operating on the interpreter as it provides access to
the variables and functions that interact strongly with the
3
Mani Butwall ITM CSE Department
4
• In the above example, sys.version is used which returns a
string containing the version of Python Interpreter with some
additional information.
• This shows how the sys module interacts with the interpreter.
6
stdin
• stdin: It can be used to get input from the command line
directly. It used is for standard input.
• It internally calls the input() method. It, also, automatically
adds ‘\n’ after each sentence.
10
Mani Butwall ITM CSE Department
11
Command Line Arguments
• Command-line arguments are those which are passed during
the calling of the program along with the calling statement. To
achieve this using the sys module, the sys module provides a
variable called sys.argv. It’s main purpose are:
• It is a list of command-line arguments.
12
Mani Butwall ITM CSE Department
13
Mani Butwall ITM CSE Department
14
Exit
• Exiting the Program
• sys.exit([arg]) can be used to exit the program. The optional
argument arg can be an integer giving the exit or another type
of object. If it is an integer, zero is considered “successful
termination”.
15
Mani Butwall ITM CSE Department
16
Working with Modules
• sys.path is a built-in variable within the sys module that
returns the list of directories that the interpreter will search
for the required module.
• When a module is imported within a Python file, the
interpreter first searches for the specified module among its
17
Mani Butwall ITM CSE Department
18
Reference Count
• sys.getrefcount() method is used to get the reference count
for any given object. This value is used by Python as when this
value becomes 0, the memory for that particular value is
deleted.
21
Function Description
getsizeof Return the size of object in bytes.>>> x = 5 >>>
sys.getsizeof(x) 28 >>> s = "hello" >>> sys.getsizeof(s) 54
22
Mani Butwall ITM CSE Department
23
Mani Butwall ITM CSE Department
24
Mani Butwall ITM CSE Department
25
Mani Butwall ITM CSE Department
26
Mani Butwall ITM CSE Department
27