看到别人执行一个支持命令行参数的python文件,瞬间觉得高大上起来、牛逼起来,那么如何编写一个带命令行参数的python脚本呢?不用紧张,下面将简单易懂地让你学会如何让自己的python脚本,支持命令行参数。

首先你要知道python中的sys模块的一些功能:

import sys

print "the number of python program's argument:",len(sys.argv)

print "the value of every argument is ",str(sys.argv)

python sysargv.py argv1 argv2 argv3 argv4

the number of python program's argument: 5

the value of every argument is ['sysargv.py', 'argv1', 'argv2', 'argv3', 'argv4']

其次,python程序使用命令行参数,必不可少的模块,就是getopt 模块,先看看一段代码

import getopt

args = '-a -b -cfoo -d bar a1 a2'.split()

args

['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']

optlist, args = getopt.getopt(args, 'abc:d:')

optlist

[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]

args

['a1', 'a2']

使用long_options

s = '--condition=foo --testing --output-file abc.def -x a1 a2'

args = s.split()

args

['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']

optlist, args = getopt.getopt(args, 'x', ['condition=', 'output-file=', 'testing'])

optlist

[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]

args

['a1', 'a2']

最后实战一个例子吧!

import getopt,sys

def main():

try:

opts,args=getopt.getopt(sys.argv[1:],"hi:o:v",["help","infile=","outfile="])

except getopt.GetoptError as error:

print str(error)

usage()

sys.exit(2)

infile=None

output=None

verbose=False

for key,value in opts:

if key=="-v":

verbose=True

elif key in ("-h","--help"):

print "sysargv.py -i -o "

print "or sysargv.py --infile --outfile "

elif key in ("-i","--infile"):

infile = value

elif key in ("-o","--outfile"):

output= value

print "inputfile:", infile

print "outputfile:", output

print verbose

if __name__=="__main__":

main()

测试结果:

C:\Python27>python sysargv.py --help

sysargv.py -i -o

or sysargv.py --infile --outfile

inputfile: None

outputfile: None

False

C:\Python27>python sysargv.py -h

sysargv.py -i -o

or sysargv.py --infile --outfile

inputfile: None

outputfile: None

False

C:\Python27>python sysargv.py -i "inputfile1" -o "ouputfile2"

inputfile: inputfile1

outputfile: ouputfile2

False

C:\Python27>python sysargv.py -i "inputfile1"

inputfile: inputfile1

outputfile: None

False

C:\Python27>python sysargv.py -o "outputfile1"

inputfile: None

outputfile: outputfile1

False

C:\Python27>python sysargv.py -o "outputfile1" -v

inputfile: None

outputfile: outputfile1

True

C:\Python27>python sysargv.py --infile "inputfile" --outfile "outputfile1" -v

inputfile: inputfile

outputfile: outputfile1

True

python编写请求参数带文件_转载:如何编写一个带命令行参数的Python文件相关推荐

  1. python将argv作为参数_在jupyter / ipython notebook中将命令行参数传递给argv

    经过大量的环顾后,我发现了非常繁琐的自定义库,但是用几行代码解决了它,我认为这些代码很漂亮.我使用nbconvert最终得到一个html报告作为输出,包含笔记本中的所有图形和降价,但是通过最小的pyt ...

  2. [转载] c语言中检查命令行参数_C中的命令行参数

    参考链接: Java中的命令行参数 c语言中检查命令行参数 Command line argument is a parameter supplied to the program when it i ...

  3. java 命令行 解析_如何在Java中解析命令行参数?

    小编典典 例如,这是你commons-cli用来解析2个字符串参数的方法: import org.apache.commons.cli.*; public class Main { public st ...

  4. java命令行参数_一个 java 命令行参数顺序的坑

    tream Stream是在Java SE 8 API添加的用于增强集合的操作接口,可以让你以一种声明的方式处理集合数据.将要处理的集合看作一种流的创建者,将集合内部的元素转换为流并且在管道中传输, ...

  5. Python 命令行参数详解

    Python 命令行参数详解 0. 命令行参数 1. sys.argv 2. getopt 2.1 getopt.getopt 方法 2.2 Exception getopt.GetoptError ...

  6. python 命令行参数-Python命令行参数处理

    sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 import ...

  7. python 命令行参数-Python3之命令行参数处理

    toc sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 imp ...

  8. python命令行输入参数_Python命令行参数处理

    sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 import ...

  9. Python教程:命令行参数处理

    sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 import ...

最新文章

  1. 线性O(N)时间复杂度求素数 , 筛法
  2. CentOS7 网络安装流程
  3. Uva 442 - Matrix Chain Multiplication(模拟)
  4. 登录方式2:windows命令行
  5. redhat 挂载 iso文件 提示 mount :not a directory
  6. 百年理工计算机专业课程,这两所国内的百年理工院校,实力强劲,都是国内顶尖实力...
  7. 带你手写基于 Spring 的可插拔式 RPC 框架(四)代理类的注入与服务启动
  8. 你会为了好工作和优质配偶整容吗?
  9. Python_软件安装
  10. linux下安装vmware tools的方法
  11. 创业12年 最野蛮的屌丝创业者
  12. 激光雷达障碍物检测:点云聚类算法
  13. 小水智能-智能楼宇智慧建筑3D可视化系统,为房屋建设增加智能化
  14. 非常精美的唐诗,无与伦比哦
  15. php获取视频信息,支持优酷土豆新浪腾讯等多家网站
  16. 【管理经验】面对重大生产事故,应该怎么办?
  17. 深圳周末好去处|深圳一日游推荐攻略
  18. 集训队每周一赛 2020-04-02(思维/模拟+贪心+二分)
  19. 多线程打印声母和韵母
  20. 电脑如何快速将桌面文件转移到其他硬盘?

热门文章

  1. 在家办公这半年,让我开始热爱生活
  2. 用.NET进行客户端Web开发?看这个Bootstrap风格的BlazorUI组件库
  3. .NET Core + Kubernetes:Pod
  4. 【Magicodes.IE 2.0.0-beta1版本发布】已支持数据表格、列筛选器和Sheet拆分
  5. C# 8.0 的默认接口方法
  6. ASP.NET Core on K8S深入学习(9)Secret Configmap
  7. 数据结构为什么那么难?
  8. WeihanLi.Npoi 导出支持自定义列内容啦
  9. ASP.NET Core 中使用IHttpClientFactory发出HTTP请求
  10. 在.net core 中PetaPoco结合EntityFrameworkCore使用codefirst方法进行开发