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字符串的encode与decode研究心得乱码问题解决方法
Mar 23 Python
Python使用xlrd读取Excel格式文件的方法
Mar 10 Python
Python中几个比较常见的名词解释
Jul 04 Python
Python高级特性切片(Slice)操作详解
Sep 27 Python
python图的深度优先和广度优先算法实例分析
Oct 26 Python
python tornado修改log输出方式
Nov 18 Python
使用python的turtle绘画滑稽脸实例
Nov 21 Python
keras多显卡训练方式
Jun 10 Python
python如何调用java类
Jul 05 Python
python中封包建立过程实例
Feb 18 Python
python opencv常用图形绘制方法(线段、矩形、圆形、椭圆、文本)
Apr 12 Python
Python Django搭建文件下载服务器的实现
May 10 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
防止MySQL注入或HTML表单滥用的PHP程序
2009/01/21 PHP
在项目中寻找代码的坏命名
2012/07/14 PHP
PHP实现的交通银行网银在线支付接口ECSHOP插件和使用例子
2014/05/10 PHP
PHP函数http_build_query使用详解
2014/08/20 PHP
php函数连续调用实例分析
2015/07/30 PHP
CSS JavaScript 实现菜单功能 改进版
2008/12/09 Javascript
使用javascript:将其它类型值转换成布尔类型值的解决方法详解
2013/05/07 Javascript
js实现数字每三位加逗号的方法
2015/02/05 Javascript
ES2015 Symbol 一种绝不重复的值
2016/12/25 Javascript
nodejs中解决异步嵌套循环和循环嵌套异步的问题
2017/07/12 NodeJs
React Native验证码倒计时工具类分享
2017/10/24 Javascript
详解Vue单元测试case写法
2018/05/24 Javascript
详解Python进阶之切片的误区与高级用法
2018/12/24 Python
详解Python3之数据指纹MD5校验与对比
2019/06/11 Python
python 缺失值处理的方法(Imputation)
2019/07/02 Python
python 使用多线程创建一个Buffer缓存器的实现思路
2020/07/02 Python
用CSS3绘制三角形的简单方法
2015/07/17 HTML / CSS
HTML5 Plus 实现手机APP拍照或相册选择图片上传功能
2016/07/13 HTML / CSS
input file上传文件样式支持html5的浏览器解决方案
2012/11/14 HTML / CSS
HTML5超文本标记语言的实现方法
2020/09/24 HTML / CSS
全球最大的房车租赁市场:Outdoorsy
2018/09/19 全球购物
2014年迎新年联欢会活动策划方案
2014/02/26 职场文书
物业管理专业自荐信
2014/07/01 职场文书
淘宝客服工作职责
2014/07/11 职场文书
二年级语文下册复习计划
2015/01/19 职场文书
旷课检讨书
2015/01/26 职场文书
行政主管岗位职责范本
2015/04/09 职场文书
2015法院个人工作总结范文
2015/05/25 职场文书
幼儿园毕业典礼园长致辞
2015/07/29 职场文书
2019年新郎保证书3篇
2019/10/17 职场文书
Python基础之教你怎么在M1系统上使用pandas
2021/05/08 Python
实操Python爬取觅知网素材图片示例
2021/11/27 Python
详解Python内置模块Collections
2022/03/22 Python
宫崎骏十大动画电影,宫崎骏好看的动画电影排名
2022/03/22 日漫
spring 项目实现限流方法示例
2022/07/15 Java/Android
前端框架ECharts dataset对数据可视化的高级管理
2022/12/24 Javascript