自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

正確掌握Python sys.arg使用方法

開發(fā) 后端
我們今天在這篇文章中將會通過一個比較簡單的示例來為大家詳細介紹一下Python sys.arg使用的具體方法,以方便大家掌握。

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

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

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

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

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

2、這個例子是簡明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': #當命令行參數(shù)為-- version,顯示版本號   
  19. print 'Version 1.2'   
  20. elif option == 'help': #當命令行參數(shù)為--help時,顯示相關(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:]: #當參數(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': #當命令行參數(shù)為-- version,顯示版本號  
  51. print 'Version 1.2'  
  52. elif option == 'help': #當命令行參數(shù)為--help時,顯示相關(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:]: #當參數(shù)為文件名時,傳入readfile,讀出其內(nèi)容  
  64. readfile(filename)  

保存程序為sample.py.我們驗證一下:#t#

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

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

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

責任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-03-04 15:17:30

Python prin

2011-04-27 16:38:31

投影機

2009-11-17 17:38:37

PHP Session

2010-03-02 14:12:30

WCF枚舉類型

2010-07-05 09:38:47

LinuxNFS

2009-12-02 14:50:25

PHP接口類inter

2010-01-20 17:47:54

VB.NET注釋

2009-11-26 18:49:54

PHP函數(shù)preg_s

2010-03-01 17:39:07

WCF Address

2010-02-23 11:06:16

WCF可信賴會話

2010-05-04 09:44:12

Oracle Trig

2011-05-05 14:01:03

投影機

2010-03-02 16:58:11

AJAX WCF服務(wù)項

2010-01-18 13:12:43

VB.NET控件數(shù)組

2024-01-07 20:10:11

Python編程語言

2010-03-04 13:37:20

Python yiel

2011-02-24 13:09:10

FireFTP

2012-01-13 09:55:54

jQuery

2017-05-27 20:00:30

Python爬蟲神器PyQuery

2009-12-24 16:36:06

WPF InkCanv
點贊
收藏

51CTO技術(shù)棧公眾號