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

开发 后端
我们今天在这篇文章中将会通过一个比较简单的示例来为大家详细介绍一下Python sys.arg使用的具体方法,以方便大家掌握。

Python编程语言中,有很多比较特殊的应用方法值得我们去细细品味。在这里我们将会为大家详细分析一下有关Python sys.arg使用的方法,希望可以给朋友们带来一些帮助,以方便在应用中的使用。

Python sys.arg使用是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从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接收命令行参数,运行参数指令,保存为sample1.py,命令行带参数运行sample1.py notepad,将打开记事本程序。

2、这个例子是简明Python教程上的,明白它之后你就明白Python sys.arg使用了。

 

  1. import sys   
  2. def readfile(filename): #从文件中读出文件内容   
  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 分别输出每行内容   
  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': #当命令行参数为-- 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)   
  33. import sys  
  34. def readfile(filename): #从文件中读出文件内容  
  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 分别输出每行内容  
  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': #当命令行参数为-- version,显示版本号  
  51. print 'Version 1.2'  
  52. elif option == 'help': #当命令行参数为--help时,显示相关帮助内容  
  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:]: #当参数为文件名时,传入readfile,读出其内容  
  64. readfile(filename)  

保存程序为sample.py.我们验证一下:#t#

< !--[if !supportLists]-->1) < !--[endif]-->命令行带参数运行:sample.py –version 输出结果为:version 1.2

< !--[if !supportLists]-->2) < !--[endif]-->命令行带参数运行:sample.py –help 输出结果为:This program prints files……

< !--[if !supportLists]-->3) < !--[endif]-->在与sample.py同一目录下,新建a.txt的记事本文件,内容为:test argv;命令行带参数运行:sample.py a.txt,输出结果为a.txt文件内容:test argv,这里也可以多带几个参数,程序会先后输出参数文件内容。以上就是我们为大家介绍的Python sys.arg使用相关方法。

责任编辑:曹凯 来源: 博客园
相关推荐

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-01-20 17:47:54

VB.NET注释

2010-07-05 09:38:47

LinuxNFS

2009-12-02 14:50:25

PHP接口类inter

2010-03-01 17:39:07

WCF Address

2010-02-23 11:06:16

WCF可信赖会话

2010-05-04 09:44:12

Oracle Trig

2009-11-26 18:49:54

PHP函数preg_s

2010-03-02 16:58:11

AJAX WCF服务项

2011-05-05 14:01:03

投影机

2010-01-18 13:12:43

VB.NET控件数组

2010-03-04 13:37:20

Python yiel

2024-01-07 20:10:11

Python编程语言

2012-01-13 09:55:54

jQuery

2011-02-24 13:09:10

FireFTP

2017-05-27 20:00:30

Python爬虫神器PyQuery

2009-12-24 16:36:06

WPF InkCanv
点赞
收藏

51CTO技术栈公众号