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实现同时给多个变量赋值的方法
Apr 30 Python
Python实现多线程HTTP下载器示例
Feb 11 Python
Python多线程实现同步的四种方式
May 02 Python
python实现读取excel写入mysql的小工具详解
Nov 20 Python
基于python实现聊天室程序
Jul 27 Python
python+pyqt5编写md5生成器
Mar 18 Python
python常见字符串处理函数与用法汇总
Oct 30 Python
Python实现清理微信僵尸粉功能示例【基于itchat模块】
May 29 Python
在pycharm中debug 实时查看数据操作(交互式)
Jun 09 Python
用python制作个视频下载器
Feb 01 Python
python3实现无权最短路径的方法
May 12 Python
Python开发五子棋小游戏
Apr 28 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
SONY ICF-SW07收音机电路分析
2021/03/02 无线电
PHP5在Apache下的两种模式的安装
2006/09/05 PHP
定制404错误页面,并发信给管理员的程序
2006/10/09 PHP
php学习之function的用法
2012/07/14 PHP
PHP Cookie的使用教程详解
2013/06/03 PHP
php实现删除指定目录下相关文件的方法
2014/10/20 PHP
推荐一款MAC OS X 下php集成开发环境mamp
2014/11/08 PHP
php+iframe 实现上传文件功能示例
2020/03/04 PHP
IE浏览器兼容Firefox的JS脚本的代码
2008/10/23 Javascript
javascript getElementsByClassName实现代码
2010/10/11 Javascript
JS中prototype关键字的功能介绍及使用示例
2013/07/21 Javascript
解析ajaxFileUpload 异步上传文件简单使用
2016/12/30 Javascript
jQuery制作图片旋转效果
2017/02/02 Javascript
Vue.js实现一个漂亮、灵活、可复用的提示组件示例
2017/03/17 Javascript
Node.js v8.0.0正式发布!看看带来了哪些主要新特性
2017/06/02 Javascript
Angularjs使用过滤器完成排序功能
2017/09/20 Javascript
vue 使用ref 让父组件调用子组件的方法
2018/02/08 Javascript
React之PureComponent的使用作用
2018/07/10 Javascript
vue.js配合$.post从后台获取数据简单demo分享
2018/08/11 Javascript
JS实现水平遍历和嵌套递归操作示例
2019/08/15 Javascript
微信小程序服务器日期格式化问题
2020/01/07 Javascript
Python返回数组/List长度的实例
2018/06/23 Python
pandas值替换方法
2018/07/10 Python
python实现傅里叶级数展开的实现
2018/07/21 Python
opencv python图像梯度实例详解
2020/02/04 Python
Django使用django-simple-captcha做验证码的实现示例
2021/01/07 Python
使用Python爬虫爬取小红书完完整整的全过程
2021/01/19 Python
微软美国官方网站:Microsoft美国
2018/05/10 全球购物
意大利网上书店:LaFeltrinelli
2020/06/12 全球购物
企业消防安全制度
2014/02/02 职场文书
工作作风懒散检讨书
2014/10/29 职场文书
党支部创先争优公开承诺书
2015/04/30 职场文书
2019年七夕情人节浪漫祝福语大全!
2019/08/08 职场文书
面试必问:圣杯布局和双飞翼布局的区别
2021/05/13 HTML / CSS
Pytorch DataLoader shuffle验证方式
2021/06/02 Python
nginx容器方式反向代理实战
2022/04/18 Servers