Python3 中使用sys.argv详解
#/usr/bin/env python
#coding:utf-8
import sys
# print(sys.argv[1])
def readFile(filename):
"""定义readFile函数,从文件中读出文件内容"""
with open(filename) as f:
while True:
line = f.readline()
if len(line) == 0:
break
print(line)
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,索引为2开始,往后取所有
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('Unknow option')
sys.exit()
else:
for filename in sys.argv[1:]:#当参数为文件名时,传入readfile,读出其内容
readFile(filename)
Python3 中使用sys.argv详解的更多相关文章
- Python3中的super()函数详解
关于Python3中的super()函数 我们都知道,在Python3中子类在继承父类的时候,当子类中的方法与父类中的方法重名时,子类中的方法会覆盖父类中的方法, 那么,如果我们想实现同时调用父类和子 ...
- opencv中 int main(int argc,char* argv[])详解
opencv中 int main(int argc,char* argv[])详解 argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数 ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- (转)python之os,sys模块详解
python之sys模块详解 原文:http://www.cnblogs.com/cherishry/p/5725184.html sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和 ...
- Objective-C中 Self和 Super详解
Objective-C中 Self和 Super详解 Objective-C 中Self 和 Super 详解本文要介绍的内容,在 Objective-C 中的类实现中经常看到这两个关键字 self ...
- oracle中的dual表详解
oracle中的dual表详解 1.DUAL表的用途 Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中 --查看当前连接用户 SQL> s ...
- ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解
ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解 1.1. 名词解释 1.2. Kestrel基本工作原理 1.2.1. Kestrel的基本架构 1.2.2. Ke ...
- [转帖]ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解
ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解 https://www.cnblogs.com/vipyoumay/p/7525478.html ASP.NET C ...
- Python3.x:sys.argv[]的简介
Python3.x:sys.argv[]的简介 sys模块通过sys.argv提供对任何命令行参数的访问.主要有两个参数变量: sys.argv是命令行参数的列表. len(sys.argv)是命令行 ...
随机推荐
- FastReport调整字体
- [eetcode 10]Regular Expression Matching
1 题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- [LintCode] Longest Increasing Continuous subsequence
http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...
- C# 读取资源文件.resx 中的xml资源
主要是以字符串的形式来读取xml,然后通过遍历读取节点,通过节点属性名称获取属性值 /// <summary> /// 初始化OPC参数配置 /// </summary> // ...
- wpf 导出Excel
private void Button_Click_1(object sender, RoutedEventArgs e) { ExportDataGridSaveAs(true, this.data ...
- 关于css中float的理解
感觉css里的float是个非常神奇的东西,神奇之处在于,你知道它是什么意思,但是用的时候总是不知道怎么实现效果.又或者它会很容易地影响到别的元素和属性.所以今天打算尝试一下float的各种设置,看看 ...
- 「PKUSC2018」最大前缀和(状压dp)
前言 考试被\(hyj\)吊着打... Solution 考虑一下如果前缀和如果在某一个位置的后面的任意一个前缀和都<=0,肯定这就是最大的. 然后这样子就考虑左右两边的状压dp,然后就好了. ...
- JQuery - 阻止回车键
JQuery 和 js 禁止enter回车事件方法 jQuery版 $(window).keydown( function(e) { var key = window.event?e.keyCode: ...
- OCP考试062题库出现大量新题-18
choose two Examine this command executed on a client that is remote from the database server. SQL> ...
- mysql多列索引
1,数据库每次查询只能使用一个索引 2,假设数据 表T (a,b,c) rowid 为物理位置rowid a b c(1) 1 1 1(2) 2 1 13(3) 2 2 14(4) 1 3 3(5) ...