0% found this document useful (0 votes)
9 views27 pages

Unit VII

Uploaded by

Dhruti Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views27 pages

Unit VII

Uploaded by

Dhruti Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Unit-VII

Mani Butwall ITM CSE Department


System Level Interaction with Python
By:
Prof. Mani Butwall,
Asst. Prof. (CSE)
1
Contents
• Reading Data Interactively
• Standard Streams
• Environment Variables

Mani Butwall ITM CSE Department


• Command Line Arguments
• Exit Status
• Running system commands in python
• Obtaining the output of a system command

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

Mani Butwall ITM CSE Department


interpreter. Let’s consider the below example.

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.

Mani Butwall ITM CSE Department


5
Input and Output using sys
• The sys modules provide variables for better control over
input or output. We can even redirect the input and output to
other devices. This can be done using three variables –

Mani Butwall ITM CSE Department


1. stdin
2. stdout
3. stderr

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.

Mani Butwall ITM CSE Department


7
Mani Butwall ITM CSE Department
8
Mani Butwall ITM CSE Department
9
stdout
• stdout: A built-in file object that is analogous to the
interpreter’s standard output stream in Python.
• stdout is used to display output directly to the screen console.
Output can be of any form, it can be output from a print
statement, an expression statement, and even a prompt direct

Mani Butwall ITM CSE Department


for input.
• By default, streams are in text mode. In fact, wherever a print
function is called within the code, it is first written to
sys.stdout and then finally on to the screen.

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.

Mani Butwall ITM CSE Department


• len(sys.argv) provides the number of command-line
arguments.
• sys.argv[0] is the name of the current Python script.
• Example: Consider a program for adding numbers and the
numbers are passed along with the calling statement.

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”.

Mani Butwall ITM CSE Department


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

Mani Butwall ITM CSE Department


built-in modules. If not found it looks through the list of
directories defined by sys.path.
• Note: sys.path is an ordinary list and can be manipulated.
• Example 1: Listing out all the paths

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.

Mani Butwall ITM CSE Department


19
Function Description
sys.setrecursionlimit() method is used to set the maximum depth
sys.setrecursionlimit()
of the Python interpreter stack to the required limit.

sys.getrecursionlimit() method is used to find the current


sys.getrecursionlimit()
recursion limit of the interpreter or to find the maximum depth of
method
the Python interpreter stack.

It is used for implementing debuggers, profilers and coverage


tools. This is thread-specific and must register the trace using
sys.settrace()
threading.settrace(). On a higher level, sys.settrace() registers the

Mani Butwall ITM CSE Department


traceback to the Python interpreter

sys.setswitchinterval() sys.setswitchinterval() method is used to set the interpreter’s


method thread switch interval (in seconds).

It fetches the largest value a variable of data type Py_ssize_t can


sys.maxsize()
store.

maxint/INT_MAX denotes the highest value that can be


sys.maxint
represented by an integer.
20
sys.getdefaultencoding() sys.getdefaultencoding() method is used to get the current default
method string encoding used by the Unicode implementation.
Function Description

platform Name of the platform on which Python is running, e.g.


"linux2" for Linux and "win32" for Windows>>> sys.platform
'linux' >>>

version Version number of the Python interpreter>>> sys.version


'3.8.5 (default, Sep 4 2020, 07:30:14) \n[GCC 7.3.0]' >>>

Mani Butwall ITM CSE Department


version_info Similar information than sys.version, but the output is a tuple
containing the five components of the version number:
major, minor, micro, release-level, and serial. The values of
this tuple are integers except the value for the release level,
which is one of the following: 'alpha', 'beta', 'candidate', or
'final'.>>> sys.version_info sys.version_info(major=3, minor=8,
micro=5, releaselevel='final', serial=0) >>>

21
Function Description
getsizeof Return the size of object in bytes.>>> x = 5 >>>
sys.getsizeof(x) 28 >>> s = "hello" >>> sys.getsizeof(s) 54

__stdin__ These attributes contain the original values of stdin, stderr


__stdout__ and stdout at the start of the program. They can be useful
__stderr__ to print to the actual standard stream no matter if the
sys.std* object has been redirected. They can also be used
to restore the actual files to known working file objects in

Mani Butwall ITM CSE Department


case they have been overwritten with a broken object. But
instead of using these values, the original stream should
always be explicitly saved in a variable (as shown in our
previous examples) before replacing it, and the original
state should be restored by using the saved object.

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

You might also like