python 命令行 参数

Python Command line arguments are input parameters passed to the script when executing them. Almost all programming language provide support for command line arguments. Then we also have command line options to set some specific options for the program.

Python命令行参数是执行它们时传递给脚本的输入参数。 几乎所有的编程语言都支持命令行参数。 然后,我们还有命令行选项可以为程序设置一些特定选项。

Python命令行参数 (Python Command Line Arguments)

There are many options to read python command line arguments. The three most common ones are:

有很多选项可以读取python命令行参数。 三种最常见的是:

  1. Python sys.argvPython sys.argv
  2. Python getopt modulePython getopt模块
  3. Python argparse modulePython argparse模块

Let’s look through simple program to learn how to read and use python command line arguments.

让我们看一个简单的程序,以学习如何阅读和使用python命令行参数。

Python sys模块 (Python sys module)

Python sys module stores the command line arguments into a list, we can access it using sys.argv. This is very useful and simple way to read command line arguments as String. Let’s look at a simple example to read and print command line arguments using python sys module.

Python sys模块将命令行参数存储到列表中,我们可以使用sys.argv进行访问。 这是将命令行参数读取为String的非常有用且简单的方法。 让我们看一个使用python sys模块读取和打印命令行参数的简单示例。

import sysprint(type(sys.argv))
print('The command line arguments are:')
for i in sys.argv:print(i)

Below image illustrates the output of a sample run of above program.

下图显示了以上程序的示例运行的输出。

There are many other useful functions in sys module, read them in detail at python sys module.

sys模块中还有许多其他有用的功能,请在python sys module上详细阅读它们。

Python getopt模块 (Python getopt module)

Python getopt module is very similar in working as the C getopt() function for parsing command-line parameters.

Python的getopt模块与C的getopt()函数用于解析命令行参数非常相似。

Python getopt module is useful in parsing command line arguments where we want user to enter some options too. Let’s look at a simple example to understand this.

Python getopt模块在解析命令行参数时非常有用,我们也希望用户也输入一些选项。 让我们看一个简单的例子来理解这一点。

import getopt
import sysargv = sys.argv[1:]
try:opts, args = getopt.getopt(argv, 'hm:d', ['help', 'my_file='])print(opts)print(args)
except getopt.GetoptError:# Print a message or do something usefulprint('Something went wrong!')sys.exit(2)

Above example is very simple, but we can easily extend it to do various things. For example, if help option is passed then print some user friendly message and exit. Here getopt module will automatically parse the option value and map them. Below image shows a sample run.

上面的示例非常简单,但是我们可以轻松地将其扩展为执行各种操作。 例如,如果通过了帮助选项,则打印一些用户友好的消息并退出。 在这里,getopt模块将自动分析选项值并映射它们。 下图显示了运行示例。

To learn more, read python getopt module.

要了解更多信息,请阅读python getopt模块 。

Python argparse模块 (Python argparse module)

Python argparse module is the preferred way to parse command line arguments. It provides a lot of option such as positional arguments, default value for arguments, help message, specifying data type of argument etc. At the very simplest form, we can use it like below.

Python argparse模块是解析命令行参数的首选方法。 它提供了很多选项,例如位置参数,参数的默认值,帮助消息,指定参数的数据类型等。以最简单的形式,我们可以像下面这样使用它。

import argparseparser = argparse.ArgumentParser()
parser.parse_args()

Below is the output from quick run of above script.

以下是上述脚本快速运行的输出。

Python argparse module provide a lot many features, you should read about them at python argparse tutorial for clear understanding.

Python argparse模块提供了许多功能,您应该在python argparse教程中阅读有关它们的详细信息。

That’s all for different options to read and parse command line arguments in python, you should decide which is the one for your specific requirements and then use it.

这就是在python中读取和解析命令行参数的不同选项的全部,您应该确定哪一种是针对您的特定要求的,然后再使用它。

References:

参考文献:

  • sys.argvsys.argv
  • getopt modulegetopt模块
  • argparse moduleargparse模块

翻译自: https://www.journaldev.com/17743/python-command-line-arguments

python 命令行 参数

python 命令行 参数_Python命令行参数相关推荐

  1. python 命令行参数_Python 命令行参数介绍

    Python 提供了 getopt 模块来获取命令行参数. Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表. len(sys.argv ...

  2. python命令行工具_python命令行工具Click快速掌握

    前言 写 Python 的经常要写一些命令行工具,虽然标准库提供有命令行解析工具 Argparse,但是写起来非常麻烦,我很少会使用它.命令行工具中用起来最爽的就是 Click,它是 Flask 的团 ...

  3. python命令行解析_python命令行解析函数

    sys.argv 在终端运行python 1.py hahah importsysprint(sys.argv) #['1.py', 'hahah'] argparse Python的命令行解析模块, ...

  4. python获取命令行参数_Python获取命令行参数的正确方法,案例详解

    最近编写一个python程序的时候,需要去获取python命令行的参数,因此这里记录下如何获取命令行参数的方法. 一.sys 模块 在 Python 中,sys 模块是一个非常常用且十分重要的模块,通 ...

  5. python处理多行字符串_python多行字符串

    Python中如何处理长代码格式化问题,如何提高格式化输出的长字符串的可读性? 当我们需要格式化输出一个很长的字符串的时候,都写在一行显得很难看,而且可读性也很差:当我们使用链式的语法写代码的时候常常 ...

  6. python中可变参数和关键字参数_python的可变参数和关键字参数(*args **kw)

    本文是来自廖大的python教程其中我一直忘记的部分.算是一个笔记把.因为日常这俩参数名字一直搞混 可变参数 在Python函数中,还可以定义可变参数.顾名思义,可变参数就是传入的参数个数是可变的,可 ...

  7. python 可变参数 关键字参数_Python之 可变参数和关键字参数

    原标题:Python之 可变参数和关键字参数 刚开始接触 python 的时候,对 python 中的 *wargs (可变参数) 和 **kwargs (关键字参数)的理解不是很透彻,看了一下 &l ...

  8. python中函数参数_Python函数的参数

    本章将介绍函数中参数的用法,更多内容请参考:Python学习指南 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道传递正确的参数,以及函数就返回 ...

  9. python函数可以按照参数名称方式传递参数_python函数传入参数(默认参数、可变长度参数、关键字参数)...

    1.python中默认缺省参数----定义默认参数要牢记一点:默认参数必须指向不变对象! 1 def foo(a,b=1):2 printa,b3 4 foo(2) #2 1 5 foo(3,100) ...

最新文章

  1. spring_IOC_自動裝配
  2. 面向对象和面向过程思想 oc
  3. Oracle技术之SYS用户对象不支持延迟段
  4. 【人工智能】人工智能是20世纪50年代中期兴起的一门新兴边缘科学
  5. qmc0文件怎么转换mp3_怎么用手机把手机里的视频转换成mp3音乐?(手机,不是电脑)...
  6. 软件项目开发计划书(大纲)
  7. 精讲!!! Web服务器基础与http协议
  8. Google Chrome修改网页背景颜色的办法
  9. C#方法讲解——飞行棋画地图
  10. 新人学习java导论
  11. stm32USB之模拟U盘
  12. METTLER TOLEDO托利多Bplus 标签格式设置教程(scale manager)
  13. 第二十九篇:故障处理流程
  14. 英媒:知名色情网站约80万名用户账号遭曝光
  15. 真爱如血第一季/全集True Blood迅雷下载
  16. os-level版本控制工具
  17. 富怡CAD计算机在哪,富怡CAD软件如何与富怡数字化仪进行连接
  18. oracle co函数,Oracle 常用函数
  19. 【ASP.NET】Global.asax与Web.config
  20. 电脑显示正在进行自动修复此计算机,win10系统开机一直显示“正在准备自动修复”无法启动的解决方法...

热门文章

  1. [转载] python zip 文件解压中文乱码问题解决
  2. hdfs居然无法正常停止
  3. 序列的修改、散列和切片
  4. Identityserver4配置证书
  5. js进阶正则表达式10-分组-多行匹配-正则对象的属性(小括号作用:分组,将小括号里面的东西看成一个整体,因为量词只对前一个字符有效)(多行匹配:m)(属性使用:reg.global)...
  6. Python描述性统计
  7. wpf的控件style
  8. Salesforce删除数据时出现Insufficient privileges的可能原因
  9. 算法学习_简单递归算法
  10. SSI(Server Side Includeds)使用详解(转载)