The module sys
Python contains some variables and functions related to the operation of Python itself in the environment in which it is running.
So actually, everything inside the sys
has very different roles - has functions that return the maximum recursion limit of Python - which has only to do with the language (sysgetrecusionlimit()
). Has functions that return the accent encoding used by default in text files (sys.getdefaultencoding
). Has functions to terminate the program immediately, from where Esti view (sys.exit()
) - have variables filled automatically with information about the last exception that happened in the program (sys.exc_info
) - In short, a lot of things, mostly advanced use - and the only way to know "what’s it for" is to see the documentation: https://docs.python.org/3/library/sys.html
Already the variable sys.argv
is one of the most used and simplest to explain -e, as well as everything else in the sys
, is not related to other things in the module.
sys.argv
is a list of strings that have the parameters passed on the command line to start your program.
So if your program was started on the terminal with the command:
python meuprograma.py aquelearquivo.txt 2
, the variable sys.argv
will be a list of strings ["meuprograma.py", "aquelearquivo.txt", "2"]
(note that all elements are always strings, even '2' being numerical). With this, Python allows any simple code to interact with the parameters passed by the command line, in addition to bringing a family name to those who program in C
, where these parameters are available for the function main
in the form of a pointer vector for strings: char **argv
- where, in C, the argument count has to come in a separate parameter - int argc
. This count is not required in Python because the arguments already come as a Python list, which "knows" its length.
Finally, another differential of sys
is that once Python is started, it is already imported automatically - it is not read separately from the disk. For the Python programmer, this is not noticeable, it has to be imported like any other module - but this is only to make the name "sys" available as a variable that can be used. Internally, if you have only the python.exe
in Windows, for example, and no library file, you will still be able to use the module sys
.
It is for many things, basically it is a lib with several functions focused mainly to work with the "settings" obtained in the current execution of a script, for example, take parameters, pick environment variables, pick up script path or where it is running from, outside that provides some control in the command line interface, which may vary by interface type or terminal. There must be exact technical "words" in this book, but in general it is this.
– Guilherme Nascimento
Could you explain to me how the program that I enclosed in the question works, I could not understand, or some place that I can understand more about the sys module.
– Maicon
Your doubt is only about the
sys.argv
or it’s also about the Python language and howfor
? Because it seems to me that you have no familiarity with language (maybe), then the explanation can go further.– Guilherme Nascimento