1. 程式人生 > 其它 >關於python中對sys.argv的理解

關於python中對sys.argv的理解

在python中,如果需要從命令列接收引數,比較常用的方法,就是使用sys模組中的argv,返回的結果是一個列表

sys.argv 官網的說明如下:

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system 
dependent whether this is a full pathname or not). If the command was executed 
using the -c command line option to the interpreter, argv[0
] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string. To loop over the standard input, or the list of files given on the command line, see the fileinput module. Note On Unix, command line arguments are passed by bytes from OS. Python decodes them with filesystem encoding and “surrogateescape” error handler. When you need original bytes, you can
get it by [os.fsencode(arg) for arg in sys.argv].

在使用前需要先匯入sys模組,如下,建立一個sys_argv.py檔案:

#!/usr/bin/env python3
#

import sys

print(sys.argv)

執行python3 ./sys_argv.py qq 5201351,結果如下:

['./sys_argv.py', 'qq', '5201351']

尊重別人的勞動成果 轉載請務必註明出處:https://www.cnblogs.com/5201351/p/15356175.html

作者:一名卑微的IT民工

出處:

https://www.cnblogs.com/5201351

本部落格所有文章僅用於學習、研究和交流目的,歡迎非商業性質轉載。

博主的文章沒有高度、深度和廣度,只是湊字數。由於博主的水平不高,不足和錯誤之處在所難免,希望大家能夠批評指出。

博主是利用讀書、參考、引用、抄襲、複製和貼上等多種方式打造成自己的文章,請原諒博主成為一個卑微的IT民工!