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设置检查点简单实现代码
Jul 01 Python
利用Python实现命令行版的火车票查看器
Aug 05 Python
Python快速从注释生成文档的方法
Dec 26 Python
jupyter notebook引用from pyecharts.charts import Bar运行报错
Apr 23 Python
python批量替换多文件字符串问题详解
Apr 22 Python
Python对象与引用的介绍
Jan 24 Python
Python实现DDos攻击实例详解
Feb 02 Python
检测python爬虫时是否代理ip伪装成功的方法
Jul 12 Python
Django查询优化及ajax编码格式原理解析
Mar 25 Python
jupyter lab的目录调整及设置默认浏览器为chrome的方法
Apr 10 Python
Python threading模块condition原理及运行流程详解
Oct 05 Python
Python OpenCV之常用滤波器使用详解
Apr 07 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
解析php中的escape函数
2013/06/29 PHP
浅析linux下apache服务器的配置和管理
2013/08/10 PHP
Laravel重写用户登录简单示例
2016/10/08 PHP
点击广告后才能获得下载地址
2006/10/26 Javascript
jQuery 扩展对input的一些操作方法
2009/10/30 Javascript
js左侧多级菜单动态的解决方案
2010/02/01 Javascript
js 遍历对象的属性的代码
2011/12/29 Javascript
自定义jQuery选项卡插件实例
2013/03/27 Javascript
Js日期选择器并自动加入到输入框中示例代码
2013/08/02 Javascript
ParseInt函数参数设置介绍
2014/01/02 Javascript
js获取判断上传文件后缀名的示例代码
2014/02/19 Javascript
js实现下拉列表选中某个值的方法(3种方法)
2015/12/17 Javascript
Bootstrap项目实战之首页内容介绍(全)
2016/04/25 Javascript
你不知道的 javascript【推荐】
2017/01/08 Javascript
js获取文件里面的所有文件名(实例)
2017/10/17 Javascript
基于jquery的on和click的区别详解
2018/01/15 jQuery
微信小程序实现折叠展开效果
2018/07/19 Javascript
JS+H5 Canvas实现时钟效果
2018/07/20 Javascript
浅谈Vue.js中如何实现自定义下拉菜单指令
2019/01/06 Javascript
Angular7中创建组件/自定义指令/管道的方法实例详解
2019/04/02 Javascript
微信小程序 搜索框组件代码实例
2019/09/06 Javascript
微信小程序iOS下拉白屏晃动问题解决方案
2019/10/12 Javascript
vue实现侧边栏导航效果
2019/10/21 Javascript
JavaScript对象访问器Getter及Setter原理解析
2020/12/08 Javascript
Python3实现带附件的定时发送邮件功能
2020/12/22 Python
itchat-python搭建微信机器人(附示例)
2019/06/11 Python
python监控nginx端口和进程状态
2019/09/06 Python
单位单身证明范本
2014/01/11 职场文书
《搭石》教学反思
2014/04/07 职场文书
法人授权委托书公证范本
2014/09/14 职场文书
合同和协议有什么区别?
2014/10/08 职场文书
门卫管理制度范本
2015/08/05 职场文书
pytorch损失反向传播后梯度为none的问题
2021/05/12 Python
swagger如何返回map字段注释
2021/07/03 Java/Android
Apache Linkis 中间件架构及快速安装步骤
2022/03/16 Servers
vue elementUI批量上传文件
2022/04/26 Vue.js