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使用in操作符时元组和数组的区别分析
May 19 Python
python通过文件头判断文件类型
Oct 30 Python
python从入门到精通(DAY 3)
Dec 20 Python
你眼中的Python大牛 应该都有这份书单
Oct 31 Python
python cs架构实现简单文件传输
Mar 20 Python
Python 获取windows桌面路径的5种方法小结
Jul 15 Python
Python3并发写文件与Python对比
Nov 20 Python
使用Python的datetime库处理时间(RPA流程)
Nov 24 Python
基于numpy中的expand_dims函数用法
Dec 18 Python
Python中import导入不同目录的模块方法详解
Feb 18 Python
Python识别html主要文本框过程解析
Feb 18 Python
Python Tkinter图形工具使用方法及实例解析
Jun 15 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
为什么《星际争霸》是测试人工智能的理想战场
2019/12/03 星际争霸
Windows IIS PHP 5.2 安装与配置方法
2009/06/08 PHP
PHP5.3安装Zend Guard Loader图文教程
2014/09/29 PHP
YII CLinkPager分页类扩展增加显示共多少页
2016/01/29 PHP
Laravel 的数据库迁移的方法
2017/07/31 PHP
Laravel5.7 Eloquent ORM快速入门详解
2019/04/12 PHP
一些有关检查数据的JS代码
2006/09/07 Javascript
NodeJS学习笔记之MongoDB模块
2015/01/13 NodeJs
jQuery中hover方法和toggle方法使用指南
2015/02/27 Javascript
JavaScript限定图片显示大小的方法
2015/03/11 Javascript
JS中完美兼容各大浏览器的scrolltop方法
2015/04/17 Javascript
javascript实现checkbox全选的代码
2015/04/30 Javascript
JS实现网页顶部向下滑出的全国城市切换导航效果
2015/08/22 Javascript
jquery实现简单实用的弹出层效果代码
2015/10/15 Javascript
深入浅析JSON.parse()、JSON.stringify()和eval()的作用详解
2016/04/03 Javascript
浅谈JavaScript前端开发的MVC结构与MVVM结构
2016/06/03 Javascript
超简单的Vue.js环境搭建教程
2017/03/17 Javascript
jQuery实现手势解锁密码特效
2017/08/14 jQuery
Nuxt.js实战详解
2018/01/18 Javascript
vue中如何动态绑定图片,vue中通过data返回图片路径的方法
2018/02/07 Javascript
Auto.js自动收取自己和好友蚂蚁森林能量脚本
2018/06/28 Javascript
JavaScript常见JSON操作实例分析
2018/08/08 Javascript
js实现录音上传功能
2019/11/22 Javascript
[01:22:29]真视界:2019年国际邀请赛总决赛
2020/01/29 DOTA
[08:08]DOTA2-DPC中国联赛2月28日Recap集锦
2021/03/11 DOTA
Python时间戳与时间字符串互相转换实例代码
2013/11/28 Python
利用Pandas 创建空的DataFrame方法
2018/04/08 Python
Python List列表对象内置方法实例详解
2019/10/22 Python
Python 异步协程函数原理及实例详解
2019/11/13 Python
python opencv如何实现图片绘制
2020/01/19 Python
python更新数据库中某个字段的数据(方法详解)
2020/11/18 Python
Django和Ueditor自定义存储上传文件的文件名
2021/02/25 Python
Styleonme中文网:韩国高档人气品牌
2017/06/21 全球购物
工作失误检讨书(经典集锦版)
2014/10/17 职场文书
作文评语怎么写
2014/12/25 职场文书
vue使用localStorage持久性存储实现评论列表
2022/04/14 Vue.js