Python argv用法詳解
想用python處理一下文件,發(fā)現(xiàn)有argv這個(gè)用法,搜來學(xué)習(xí)一下。
如果想對(duì)python腳步傳參數(shù),那么就需要命令行參數(shù)的支持了,這樣可以省的每次去改腳步了。
用法是:python xx.py xxx
舉例如下:
#-*- coding:utf- -*- from sys import argv script,first = argv print "the script is called:", script print "the first variable is:", first
這里argv接收到的是一個(gè)列表變量
#-*- coding:utf- -*- from sys import argv f = open(argv[], 'r') print f.read() f.close()
比方說這里我讀取文件名,開始寫成了 open(argv, 'r'),會(huì)提示類型錯(cuò)誤,改成argv[1]就好了
下面再來詳細(xì)介紹下sys.argv[]用法
Sys.argv[]是用來獲取命令行參數(shù)的,sys.argv[0]表示代碼本身文件路徑,所以參數(shù)從1開始,以下兩個(gè)例子說明:
1、使用sys.argv[]的一簡單實(shí)例,
import sys,os os.system(sys.argv[1])
這個(gè)例子os.system接收命令行參數(shù),運(yùn)行參數(shù)指令,保存為sample1.py,命令行帶參數(shù)運(yùn)行sample1.py notepad,將打開記事本程序。
2、這個(gè)例子是簡明python教程上的,明白它之后你就明白sys.argv[]了。
import sys
def readfile(filename): #從文件中讀出文件內(nèi)容
'''''Print a file to the standard output.'''
f = file(filename)
while True:
line = f.readline()
if len(line) == 0:
break
print line, # notice comma 分別輸出每行內(nèi)容
f.close()
# Script starts from here
if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
# fetch sys.argv[1] but without the first two characters
if option == 'version': #當(dāng)命令行參數(shù)為-- version,顯示版本號(hào)
print 'Version 1.2'
elif option == 'help': #當(dāng)命令行參數(shù)為--help時(shí),顯示相關(guān)幫助內(nèi)容
print '''''/
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help'''
else:
print 'Unknown option.'
sys.exit()
else:
for filename in sys.argv[1:]: #當(dāng)參數(shù)為文件名時(shí),傳入readfile,讀出其內(nèi)容
readfile(filename)
保存程序?yàn)閟ample.py.我們驗(yàn)證一下:
1) 命令行帶參數(shù)運(yùn)行:sample.py –version 輸出結(jié)果為:version 1.2
2) 命令行帶參數(shù)運(yùn)行:sample.py –help 輸出結(jié)果為:This program prints files……
3) 在與sample.py同一目錄下,新建a.txt的記事本文件,內(nèi)容為:test argv;命令行帶參數(shù)運(yùn)行:sample.py a.txt,輸出結(jié)果為a.txt文件內(nèi)容:test argv,這里也可以多帶幾個(gè)參數(shù),程序會(huì)先后輸出參數(shù)文件內(nèi)容。
相關(guān)文章
Python實(shí)現(xiàn)文件只讀屬性的設(shè)置與取消
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)設(shè)置文件只讀與取消文件只讀的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07
Python3爬蟲里關(guān)于Splash負(fù)載均衡配置詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲里關(guān)于Splash負(fù)載均衡配置的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07
Python語言檢測模塊langid和langdetect的使用實(shí)例
今天小編就為大家分享一篇關(guān)于Python語言檢測模塊langid和langdetect的使用實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
python中的TCP(傳輸控制協(xié)議)用法實(shí)例分析
這篇文章主要介紹了python中的TCP(傳輸控制協(xié)議)用法,結(jié)合完整實(shí)例形式分析了Python基于TCP協(xié)議的服務(wù)器端與客戶端相關(guān)實(shí)現(xiàn)技巧及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11
Python 模擬員工信息數(shù)據(jù)庫操作的實(shí)例
下面小編就為大家?guī)硪黄狿ython 模擬員工信息數(shù)據(jù)庫操作的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
用Python的線程來解決生產(chǎn)者消費(fèi)問題的示例
這篇文章主要介紹了用Python的線程來解決生產(chǎn)者消費(fèi)問題的示例,包括對(duì)使用線程中容易出現(xiàn)的一些問題給出了相關(guān)解答,需要的朋友可以參考下2015-04-04

