python sys.argv[]用法实例详解


Posted in Python onMay 25, 2018

sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始,以下两个例子说明:

1、使用sys.argv[]的一简单实例:

以下是sample1.py文件:

import sys,os  
print sys.argv 
os.system(sys.argv[1])

这个例子os.system接收命令行参数,运行参数指令,cmd命令行带参数运行python sample1.py notepad,将打开记事本程序。

2、这个例子是简明python教程上的,明白它之后你就明白sys.argv[]了。

以下是sample.py文件:

#!/usr/bin/env python  
#_*_ coding:utf-8 _*_  
import sys   
def readfile(filename): #定义readfile函数,从文件中读出文件内容   
  '''''''''Print a file to the standard output.'''   
  f = file(filename)   
  while True:   
    line = f.readline()   
    if len(line) == 0:   
      break   
    print line, # notice comma 分别输出每行内容   
  f.close()   
# Script starts from here  
print sys.argv  
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': #当命令行参数为-- version,显示版本号   
    print 'Version 1.2'   
  elif option == 'help': #当命令行参数为--help时,显示相关帮助内容   
    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:]: #当参数为文件名时,传入readfile,读出其内容   
    readfile(filename)

在与sample.py同一目录下,新建3个记事本文件test.txt,test1.txt,test2.txt,内容如下图:    

python sys.argv[]用法实例详解               python sys.argv[]用法实例详解              python sys.argv[]用法实例详解                   

验证sample.py,如下:

C:\Users\91135\Desktop>python sample.py
 ['sample.py']
No action specified.
C:\Users\91135\Desktop>python sample.py --help
['sample.py', '--help']
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
C:\Users\91135\Desktop>python sample.py --version
 ['sample.py', '--version']
Version 1.2
C:\Users\91135\Desktop>python sample.py --ok
 ['sample.py', '--ok']
Unknown option.
C:\Users\91135\Desktop>python sample.py test.txt
 ['sample.py', 'test.txt']
hello python!
C:\Users\91135\Desktop>python sample.py test.txt test1.txt test2.txt
 ['sample.py', 'test.txt', 'test1.txt', 'test2.txt']
 hello python!
 hello world!
hello wahaha!
goodbye!
C:\Users\91135\Desktop>

总结

以上所述是小编给大家介绍的python sys.argv[]用法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python操作MySQL数据库的方法分享
May 29 Python
python制作最美应用的爬虫
Oct 28 Python
Python正规则表达式学习指南
Aug 02 Python
Python实现登陆文件验证方法
Oct 06 Python
Python数据可视化之画图
Jan 15 Python
Python中print和return的作用及区别解析
May 05 Python
python基于json文件实现的gearman任务自动重启代码实例
Aug 13 Python
OpenCV哈里斯(Harris)角点检测的实现
Jan 15 Python
详解django中Template语言
Feb 22 Python
python中rc1什么意思
Jun 19 Python
使用Selenium实现微博爬虫(预登录、展开全文、翻页)
Apr 13 Python
Python如何让字典保持有序排列
Apr 29 Python
python切片及sys.argv[]用法详解
May 25 #Python
windows下python安装pip图文教程
May 25 #Python
python3.6使用pymysql连接Mysql数据库
May 25 #Python
python matplotlib绘图,修改坐标轴刻度为文字的实例
May 25 #Python
Python二叉树定义与遍历方法实例分析
May 25 #Python
matplotlib 纵坐标轴显示数据值的实例
May 25 #Python
对python中Matplotlib的坐标轴的坐标区间的设定实例讲解
May 25 #Python
You might like
国产动画《伍六七》原声大碟大卖,啊哈娱乐引领音乐赋能IP的新尝试
2020/03/08 国漫
php a simple smtp class
2007/11/26 PHP
一些 PHP 管理系统程序中的后门
2009/08/05 PHP
PHP捕获Fatal error错误的方法
2014/06/11 PHP
VB中的RasEnumConnections函数返回632错误解决方法
2014/07/29 PHP
php jsonp单引号转义
2014/11/23 PHP
jquery动态改变form属性提交表单
2014/06/03 Javascript
jQuery DOM删除节点操作指南
2015/03/03 Javascript
深入学习JavaScript中的原型prototype
2015/08/13 Javascript
基于replaceChild制作简单的吞噬特效
2015/09/21 Javascript
JS实现单击输入框弹出选择框效果完整实例
2015/12/14 Javascript
使用jquery获取url以及jquery获取url参数的实现方法
2016/05/25 Javascript
利用js编写响应式侧边栏
2016/09/17 Javascript
基于js中style.width与offsetWidth的区别(详解)
2017/11/12 Javascript
基于js 各种排序方法和sort方法的区别(详解)
2018/01/03 Javascript
官方推荐react-navigation的具体使用详解
2018/05/08 Javascript
vue服务端渲染页面缓存和组件缓存的实例详解
2018/09/18 Javascript
超详细动手搭建一个VuePress 站点及开启PWA与自动部署的方法
2019/01/27 Javascript
微信小程序如何调用新闻接口实现列表循环
2019/07/02 Javascript
[30:37]【全国守擂赛】第三周擂主赛 Dark Knight vs. Leopard Gaming
2020/05/04 DOTA
Python中的jquery PyQuery库使用小结
2014/05/13 Python
Python读写ini文件的方法
2015/05/28 Python
Python使用dis模块把Python反编译为字节码的用法详解
2016/06/14 Python
python用BeautifulSoup库简单爬虫实例分析
2018/07/30 Python
Python GUI布局尺寸适配方法
2018/10/11 Python
Pycharm学生免费专业版安装教程的方法步骤
2020/09/24 Python
详解HTML5 Canvas绘制时指定颜色与透明度的方法
2016/03/25 HTML / CSS
html5唤醒APP小记
2019/03/27 HTML / CSS
矫正人员思想汇报
2014/01/08 职场文书
计算机专业职业规划
2014/02/28 职场文书
群众路线自查自纠工作情况报告
2014/10/28 职场文书
建筑工程催款函
2015/06/24 职场文书
八年级数学教学反思
2016/02/17 职场文书
css3带你实现3D转换效果
2022/02/24 HTML / CSS
Python实现归一化算法详情
2022/03/18 Python
微信小程序 根据不同用户切换不同TabBar
2022/04/21 Javascript