Hello everyone, welcome to watch my video. In this video, I will show you how to use
Python SysModel to receive a command line argument in Python program. In Python, we use the
SysModel's argv list to get all the command line arguments. So first, you should import the SysModel
and then use the argv list to receive the command line arguments. Now let's see an example
This is a function, I define it, and the SysModel argv is a list. It contains all the
command line arguments you pass to this Python program. First, it will get the list of lines
and then print out the list of lines. Then, loop in the list, loop in the argument list
and get each one of the arguments and print out the argument on the screen
We can run this example first. We can set up a Python and debug it. Click run, start debugging
It stops here because we do not pass any argument to the testSysArg.py, so there are zero
arguments, but the argv list is not empty. It contains only one element, which is this
Python.file.absolute.path, so the length is one. We loop in the argv and print out the
execute the Python.file.absolute.path. Okay, so how can we pass arguments to this
Python program? Let me show you. Click terminal, no terminal. Now we use the Python command and
input this Python.file.absolute.path. We pass some parameters to it, for example, one
helloPython, okay, a, b, c, press enter. Then you can see there are four arguments. The first one in the
argv is the Python.file.absolute.path, and then the arguments we pass to this Python.file
All the elements in the argv list is a string. Okay, now we can see another example
In this example, we define a function readFileMany. This function will receive the
looping input command line arguments and looping it. For the first element
because it is a Python.file, so we print out the Python.file.absolute.path. For the parameter
after we pass to it, it will print out the parameter and trace the parameter and the file
and call another function readFile to get the file content line by line and print out
on the screen. So we can run it now. Open a terminal. Okay. And we can pass Python to this
Python.file. For example, we want to read out the content in this test.py. So this Python.file will
receive this file path and read the content from it. Okay, now we press enter. Yes
the file is, this is the excluded Python.file.absolute.path. The argument is this one
the test.py.file.absolute.path. And this is the content in the file. Okay. That's all for this video. You are welcome to subscribe my channel and I will create more
Python program video for you. Bye-bye