學習C語言的時候就沒弄明白命令行參數的用法,在學習Pyton 的時候又遇到了命令行參數,在這裏稍微學習了一下,稍微明白了一些在這裏作個記錄方便後面回顧複習。python
Sys.argv[]是用來獲取命令行參數的,sys.argv[0]表示代碼文件絕對路徑,因此因此其他參數從1開始,如下兩個例子說明:數組
一、函數
import sys,os
os.system(sys.argv[1])
這個例子os.system接收命令行參數,運行參數指令,保存爲sample1.py,命令行帶參數運行sample1.py notepad,將打開記事本程序。學習
二、這個例子是簡明python教程上的,明白它以後你就明白sys.argv[]了。fetch
1. import sys 2. def readfile(filename): #從文件中讀出文件內容 ,這是被調函數但被傳參數爲文件名de時候調用該函數 3. '''''Print a file to the standard output.''' 4. f = file(filename) #打開文件,這裏和open效果同樣 5. while True: 6. line = f.readline() #讀取每行信息 7. if len(line) == 0: 8. break 9. print line, # notice comma 分別輸出每行內容 10. f.close() 11. # Script starts from here 這裏纔是程序的入口,先判斷是否有命令行參數傳入小於二也就是沒有命令行參數了 12. if len(sys.argv) < 2: 13. print 'No action specified.' 14. sys.exit() 15. if sys.argv[1].startswith('--'): #判斷命令行參數是不是以--開始的,若是是則是相關命令的參數,若是不是就是文件名了 16. option = sys.argv[1][2:] #這是一個二維數組,把argv[1]這個參數的下標爲[2]即第三個字符開始直到結束的字符串
#複製給option 若是是--help,則這以後option爲help 17. # fetch sys.argv[1] but without the first two characters 18. if option == 'version': #當命令行參數爲-- version,顯示版本號 19. print 'Version 1.2' 20. elif option == 'help': #當命令行參數爲--help時,顯示相關幫助內容 及打印註釋的相關內容 21. print '''''/ 22. This program prints files to the standard output. 23. Any number of files can be specified. 24. Options include: 25. --version : Prints the version number 26. --help : Display this help''' 27. else: #不然打印不知道的命令 28. print 'Unknown option.' 29. sys.exit() 30. else: #若是命令行參數不是以--格式開始的,那麼就是文件了因此執行西段代碼 31. for filename in sys.argv[1:]: #當參數爲文件名時,傳入readfile,讀出其內容 32. readfile(filename)
保存程序爲sample.py.咱們驗證一下:this
1) 命令行帶參數運行:sample.py –-version 輸出結果爲:version 1.2google
2) 命令行帶參數運行:sample.py –-help 輸出結果爲:This program prints files……url
3) 在與sample.py同一目錄下,新建a.txt的記事本文件,內容爲:test argv;命令行帶參數運行:sample.py a.txt,輸出結果爲a.txt文件內容:test argv,這裏也能夠多帶幾個參數,程序會前後輸出參數文件內容。spa
1 #coding:utf-8 2 ''' 3 Created on 2015年8月10日 4 5 @author: yj 6 ''' 7 import sys 8 def skip_header(r): 9 '''Skip the reader in reader r,and return the first real piece of data.''' 10 #Read the description line and then the comment lines.讀取描述和註釋行 11 line =r.readline() 12 print line 13 while line.startswith('#'): 14 line=r.readline() #跳到下一行 15 #Now line contains the first raal piece of data. 16 return line 17 def process_file(r): 18 '''Read and print open reader r.''' 19 #Find and print piece of data. 20 line=skip_header(r).strip() 21 print line 22 #Read the rest of the data. 23 for line in r: 24 line=line.strip() 25 print line 26 if __name__=="__main__": 27 print "start" 28 input_file=open(sys.argv[1],'r') 29 process_file(input_file) 30 input_file.close()
三、上面的例子用到了一個函數startswith()命令行
函數:startswith()
做用:判斷字符串是否以指定字符或子字符串開頭
1、函數說明
語法:string.startswith(str, beg=0,end=len(string))
或string[beg:end].startswith(str)
參數說明:
string: 被檢測的字符串
str: 指定的字符或者子字符串。(能夠使用元組,會逐一匹配)
beg: 設置字符串檢測的起始位置(可選)
end: 設置字符串檢測的結束位置(可選)
若是存在參數 beg 和 end,則在指定範圍內檢查,不然在整個字符串中檢查
返回值
若是檢測到字符串,則返回True,不然返回False。默認空字符爲True
函數解析:若是字符串string是以str開始,則返回True,不然返回False
實例:
s = 'hello good boy doiido' print s.startswith('h') print s.startswith('hel') print s.startswith('h',4) print s.startswith('go',6,8) #匹配空字符集 print s.startswith('') #匹配元組 print s.startswith(('t','b','h')) 經常使用環境:用於if判斷 if s.startswith('hel'): print "you are right" else: print "you are wrang"
四、上面的程序用到了strip()函數下面簡單介紹一下
注意:strip()函數自己並不影響原來字符串的內容,獲得的僅僅是一個新的字符串,與原來的字符串無關
函數原型
聲明:s爲字符串,rm爲要刪除的字符序列
s.strip(rm) 刪除s字符串中開頭、結尾處,位於 rm刪除序列的字符
s.lstrip(rm) 刪除s字符串中開頭處,位於 rm刪除序列的字符
s.rstrip(rm) 刪除s字符串中結尾處,位於 rm刪除序列的字符
注意:
a. 當rm爲空時,默認刪除空白符(包括'\n', '\r', '\t', ' ')
例如:
複製代碼 代碼以下:
>>> a = ' 123'
>>> a.strip()
'123'
>>> a='\t\tabc'
'abc'
>>> a = 'sdff\r\n'
>>> a.strip()
'sdff'
b.這裏的rm刪除序列是隻要邊(開頭或結尾)上的字符在刪除序列內,就刪除掉。
例如 :
複製代碼 代碼以下:
>>> a = '123abc'
>>> a.strip('21')
'3abc' 結果是同樣的
>>> a.strip('12')
'3abc'
五、函數split()
說明:
Python中沒有字符類型的說法,只有字符串,這裏所說的字符就是隻包含一個字符的字符串!!!
這裏這樣寫的緣由只是爲了方便理解,僅此而已。
a、按照某個字符分割如「."
1 str = 'www.baidu.com' 2 #這裏的str.split('.')的到的是新的字符串,並不影響原來的str字符串 3 str1 = str.split('.') 4 5 print str1 6 print str 7 8 #結果爲['www','baidu','com'] 'www.baidu.com'
b、按某一個字符分割,且分割n次。如按‘.’分割1次
1 str = ('www.google.com') 2 print str 3 str_split = str.split('.',1) 4 print str_split 5 #結果:['www','google.com']
c、按某一字符(或字符串)分割,且分割n次,並將分割的完成的字符串(或字符)賦給新的(n+1)個變量。(注:見開頭說明)
如:按‘.’分割字符,且分割1次,並將分割後的字符串分別賦給2個變量str1,str2
1 url = ('www.google.com') 2 str1, str2 = url.split('.', 1) 3 print str1 4 print str2 5 6 #結果爲:str1:www str2:google.com