av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

正確掌握Pythonsys.arg使用方法

在Python編程語言中,有很多比較特殊的應(yīng)用方法值得我們?nèi)ゼ?xì)細(xì)品味。在這里我們將會為大家詳細(xì)分析一下有關(guān)Python sys.arg使用的方法,希望可以給朋友們帶來一些幫助,以方便在應(yīng)用中的使用。

目前創(chuàng)新互聯(lián)建站已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、撫遠(yuǎn)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

Python sys.arg使用是用來獲取命令行參數(shù)的,sys.argv[0]表示代碼本身文件路徑,所以參數(shù)從1開始,以下兩個(gè)例子說明:

1、使用sys.argv[]的一簡單實(shí)例,

 
 
 
  1. import sys,os   
  2. os.system(sys.argv[1])   
  3. import sys,os  
  4. os.system(sys.argv[1]) 

這個(gè)例子os.system接收命令行參數(shù),運(yùn)行參數(shù)指令,保存為sample1.py,命令行帶參數(shù)運(yùn)行sample1.py notepad,將打開記事本程序。

2、這個(gè)例子是簡明Python教程上的,明白它之后你就明白Python sys.arg使用了。

 
 
 
  1. import sys   
  2. def readfile(filename): #從文件中讀出文件內(nèi)容   
  3. '''''Print a file to the standard output.'''   
  4. f = file(filename)   
  5. while True:   
  6. line = f.readline()   
  7. if len(line) == 0:   
  8. break   
  9. print line, # notice comma 分別輸出每行內(nèi)容   
  10. if.close()   
  11. # Script starts from here   
  12. if len(sys.argv) < 2:   
  13. print 'No action specified.'   
  14. sys.exit()   
  15. 15if sys.argv[1].startswith('--'):   
  16. option = sys.argv[1][2:]   
  17. # fetch sys.argv[1] but without the first two characters   
  18. if option == 'version': #當(dāng)命令行參數(shù)為-- version,顯示版本號   
  19. print 'Version 1.2'   
  20. elif option == 'help': #當(dāng)命令行參數(shù)為--help時(shí),顯示相關(guān)幫助內(nèi)容   
  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:]: #當(dāng)參數(shù)為文件名時(shí),傳入readfile,讀出其內(nèi)容   
  32. readfile(filename)   
  33. import sys  
  34. def readfile(filename): #從文件中讀出文件內(nèi)容  
  35. '''Print a file to the standard output.'''  
  36. f = file(filename)  
  37. while True:  
  38. line = f.readline()  
  39. if len(line) == 0:  
  40. break  
  41. print line, # notice comma 分別輸出每行內(nèi)容  
  42. f.close()  
  43. # Script starts from here  
  44. if len(sys.argv) < 2: 
  45. print 'No action specified.'  
  46. sys.exit()  
  47. if sys.argv[1].startswith('--'):  
  48. option = sys.argv[1][2:]  
  49. # fetch sys.argv[1] but without the first two characters  
  50. if option == 'version': #當(dāng)命令行參數(shù)為-- version,顯示版本號  
  51. print 'Version 1.2'  
  52. elif option == 'help': #當(dāng)命令行參數(shù)為--help時(shí),顯示相關(guān)幫助內(nèi)容  
  53. print '''\  
  54. This program prints files to the standard output.  
  55. Any number of files can be specified.  
  56. Options include:  
  57. --version : Prints the version number  
  58. --help : Display this help'''  
  59. else:  
  60. print 'Unknown option.'  
  61. sys.exit()  
  62. else:  
  63. for filename in sys.argv[1:]: #當(dāng)參數(shù)為文件名時(shí),傳入readfile,讀出其內(nèi)容  
  64. readfile(filename)  

保存程序?yàn)閟ample.py.我們驗(yàn)證一下:#t#

< !--[if !supportLists]-->1) < !--[endif]-->命令行帶參數(shù)運(yùn)行:sample.py –version 輸出結(jié)果為:version 1.2

< !--[if !supportLists]-->2) < !--[endif]-->命令行帶參數(shù)運(yùn)行:sample.py –help 輸出結(jié)果為:This program prints files……

< !--[if !supportLists]-->3) < !--[endif]-->在與sample.py同一目錄下,新建a.txt的記事本文件,內(nèi)容為:test argv;命令行帶參數(shù)運(yùn)行:sample.py a.txt,輸出結(jié)果為a.txt文件內(nèi)容:test argv,這里也可以多帶幾個(gè)參數(shù),程序會先后輸出參數(shù)文件內(nèi)容。以上就是我們?yōu)榇蠹医榻B的Python sys.arg使用相關(guān)方法。


文章標(biāo)題:正確掌握Pythonsys.arg使用方法
新聞來源:http://uogjgqi.cn/article/cdighpg.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流