Sys Module in Python with Examples
Python is interesting when the coder has a logic for each code he does rather than a default function, but a defined logic makes a coder and the code even better.
With this in mind, we are going to start what predefined logic has been given by the coders initially for the learners not only to grasp more but also to learn better.
Sys module is one of them, reading the whole article you’ll surely reach out one conclusion that ‘ yeah, it couldn’t be much better’. So let’s start.
What is Sys Module in Python?
Sys module in Python basically provides access to some variables used or maintained by the interpreter and some functions that interact strongly with the interpreter. It is always available in the code library.
It provides information about constants, functions, and methods of the interpreter. But another possibility is the help() function.
You can also change the output behavior of the Python shell interactively and even in calculation, by simply rebinding (sys.displayhook) into a callable object.
Using help(sys) provides valuable detailed information about the interpreter and function.
Basic Code of sys() in Python
Import sys print(“Welcome{}toTechVidvan{}”.format(sys.argv[1], sys.argv[2]))
Output:
Now you must be wondering what is sys.argv()? No problem indeed, the fairy basket has an answer to all of this, without wasting much time, let’s get started.
Command-Line Arguments in Python sys Module
import sys print(sys.argv) for K in range(len(sys.argv)): if K==0: print("The function is",sys.argv[0]) else: print("Argument:",sys.argv[K])
Sys.exit: This causes the program to exit in a runtime freeing the space occupied.
sys.exit(return_file(name))
Sys.maxsize: Returns to the largest integer a variable can hold.
Sys.maxsize: int(input(“enter size”))
Sys.path: This basically recollects and refunctions all the paths used in a module.
Sys.version: This is an attribute that displays a string containing the version number of the current Python interpreter.
Sys.argv: This is operating system dependent, and it is mainly used in algebraic codes.
[os.fsencode(arg) for arg in sys.argv]
Sys.audit: This raises an auditing lookup in a code.
sys.addaudithook
Sys.exit([arg]): This helps in exiting the algebraic code from python.
sys.exit(“some error message”)
Sys.float_info: A tuple holding information about the float type. It has low level information about the precision and internal representation.
Sys.float int(input(“enter a number”))
sys.getrefcount(object): Returns the reference count of the object.count and it is returned one higher than you might expect.
getrefcount().
sys.getprofile(): Get the profiler function by setprofile().
sys.gettrace(): Get the trace function by settrace().
sys.intern(string): This function basically provides a platform to the user to enter the string and execute any function on it.
Sys.modules: This has a collection of all modules which are in built in python code. It can be manipulated to force reloading of modules and other tricks.
Copyright in sys()
sys.copyright in code displays the copyright information on the installed version of Python.
>>>>print(sys.copyright)
Output:
All Rights are Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights are Reserved.
Python sys.getrefcount
This python sys module method returns count for references to the object where it is used. Python keeps track of it’s value, as, when this value reaches 0 in a program, the memory for this variable is cleaned up.
Extracting Names of Modules
Sys has a collection of modules in its library, a coder can extract any module within the run time by:
>>> import sys >>> sys.modules
Exiting Flow of Execution in Python sys Module
the sys.exit in Python lets the interpreter abruptly exit the current flow of execution of the code.
>>> sys.exit
Redirecting the Output in Python sys()
Instead of delivering the output to the console, the coder can log into a text file.
Code:
>>> import sys >>> print('Welcome to TechVidan') #Prints normally
Output:
Redirecting the Error Information in Python sys Module
Using sys.stderr and text file in Python, you can log error information to the text file.
>>> import sys >>> fopen=open('error.log','w') #Opening the file >>> sys.stderr=fopen #Redirecting standard error by assigning file object of file to stderr >>> raise Exception('this is an error') >>> fopen.close()
Summary
Too much to learn, huh?
Of course, coding requires a lot of memory, when and where to use which function, it is a skill every good coder has.
Though learning so many functions of a sys module can be tiring but practicing them isn’t. Because it’s not the end, there is so much more sys() has to offer and a lot more python.
The above functions were some of the basic and necessary ones a coder should know, so get yourself started by practicing them!!