- Mastering OpenCV 4 with Python
- Alberto Fernández Villán
- 304字
- 2021-07-02 12:07:16
sys.argv
To handle command-line arguments, Python uses sys.argv. In this sense, when a program is executed, Python takes all the values from the command line and sets them in the sys.argv list. The first element of the list is the full path to the script (or the script name—it is operating system dependent), which is always sys.argv[0]. The second element of the list is the first argument to the script, which is sys.argv[1], and so on. This can be seen in the following diagram, where the sysargv_python.py script is executed with two arguments:

To see how sys.argv works, we are going to use the sysargv_python.py script:
# Import the required packages
import sys
# We will print some information in connection with sys.argv to see how it works:
print("The name of the script being processed is: '{}'".format(sys.argv[0]))
print("The number of arguments of the script is: '{}'".format(len(sys.argv)))
print("The arguments of the script are: '{}'".format(str(sys.argv)))
If we execute this script without any parameter, we will see the following information:
The name of the script being processed is: 'sysargv_python.py'
The number of arguments of the script is: '1'
The arguments of the script are: '['sysargv_python.py']'
Additionally, if we execute this script with one parameter (for example, sysargv_python.py OpenCV), we will get the following information:
The name of the script being processed is: 'sysargv_python.py'
The number of arguments of the script is: '2'
The arguments of the script are: '['sysargv_python.py', 'OpenCV']'
As you can see, the first element, sysargv_python.py (sys.argv[0]), of the list is the script name. The second element, OpenCV, of the list (sys.argv[1]) is the first argument to our script.
- Mastering OpenLayers 3
- Mastering Concurrency Programming with Java 8
- Django+Vue.js商城項目實戰
- Docker技術入門與實戰(第3版)
- 編程的修煉
- 數據結構習題解析與實驗指導
- 領域驅動設計:軟件核心復雜性應對之道(修訂版)
- Android項目實戰:手機安全衛士開發案例解析
- .NET 4.5 Parallel Extensions Cookbook
- SQL Server 入門很輕松(微課超值版)
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- Python編程快速上手2
- Monitoring Docker
- Moodle 3.x Developer's Guide
- R語言編程基礎