iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python中的 getopt模块
  • 588
分享到

Python中的 getopt模块

模块Pythongetopt 2023-01-31 05:01:05 588人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

python 的 getopt 模块是一个简单实用的命令行参数解析模块。实现命令解析功能的为模块中的getopt 方法。下面主要介绍一下这个getopt方法的使用。查看getopt 模块的帮助可以得到 getopt方法的所有解释。    g

python 的 getopt 模块是一个简单实用的命令行参数解析模块。实现命令解析功能的为模块中的getopt 方法。下面主要介绍一下这个getopt方法的使用。


查看getopt 模块的帮助可以得到 getopt方法的所有解释。

    getopt(args, shortopts, lonGopts=[])
        getopt(args, options[, long_options]) -> opts, args
        
        Parses command line options and parameter list.  args is the
        argument list to be parsed, without the leading reference to the
        running program.  Typically, this means "sys.argv[1:]".  shortopts
        is the string of option letters that the script wants to
        recognize, with options that require an argument followed by a
        colon (i.e., the same fORMat that Unix getopt() uses).  If
        specified, longopts is a list of strings with the names of the
        long options which should be supported.  The leading '--'
        characters should not be included in the option name.  Options
        which require an argument should be followed by an equal sign
        ('=').
        
        The return value consists of two elements: the first is a list of
        (option, value) pairs; the second is the list of program arguments
        left after the option list was stripped (this is a trailing slice
        of the first argument).  Each option-and-value pair returned has
        the option as its first element, prefixed with a hyphen (e.g.,
        '-x'), and the option argument as its second element, or an empty
        string if the option has no argument.  The options occur in the
        list in the same order in which they were found, thus allowing
        multiple occurrences.  Long and short options may be mixed.

 

getopt 方法的参数解释:

args 参数,在这个方法中会获取 sys.argv[1:] 对应的所有命令行参数。

shortopts 代表短参数。接收字符串类型的参数。若参数后面需要填写参数值,则使用冒号(:)表示。

example:

"hp:u:" 则表示参数 -h 且后面无需参数值。而参数 -p 后面需要参数值。同样参数 -u 也需要参数值。

longopts 代表长参数。 接收list类型的参数。后参数后面需要填写参数值,这使用等号(=)表示。

example:

["help","port=","user="] 则表示参数--help 后面无需填写参数值。而参数--port后面需要填写参数值,同样--user 后面也需要填写参数值。


getopt 方法的返回值解释:

getopt 方法返回两个list

第一个list 以 [(option,value),(option,value)] 的形式表示命令好中定义的参数

第二个参数 也是以list 的方式,它返回的是没有经过定义的参数,且这些参数必须在定义参数值后出现。若在定义参数中间出现,getopt方法会认为这之后的全部是为定义的参数。


使用方法举例:

#! /usr/bin/env Python

import getopt
import sys


if __name__ == "__main__":
    try:
        options,args = getopt.getopt(sys.argv[1:],"hp:u:",["help","port=","user="])
    except getopt.GetoptError:
        sys.exit()

    print options
    print args


做如下执行:

$ python testgetopt.py -h --port 80 --user www aa bb cc
[('-h', ''), ('--port', '80'), ('--user', 'www')]
['aa', 'bb', 'cc']

其中 aa bb cc 都是为定义的参数,放置在定义参数值后。


若做如下执行:

$ python testgetopt.py -h --port 80 aa --user www bb cc
[('-h', ''), ('--port', '80')]
['aa', '--user', 'www', 'bb', 'cc']

将为定义的参数 aa 放置到了已定义参数 --user 之前,这getopt认为 aa 及之后的所有参数都是未定义的。这种现象显然是不OK的,因此应该避免使用这样的形式。


--结束END--

本文标题: Python中的 getopt模块

本文链接: https://www.lsjlt.com/news/188576.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python中的 getopt模块
    python 的 getopt 模块是一个简单实用的命令行参数解析模块。实现命令解析功能的为模块中的getopt 方法。下面主要介绍一下这个getopt方法的使用。查看getopt 模块的帮助可以得到 getopt方法的所有解释。    g...
    99+
    2023-01-31
    模块 Python getopt
  • python getopt模块
     getopt模块用于抽出命令行选项和参数,也就是sys.argv。命令行选项使得程序的参数更加灵活。支持短选项模式和长选项模式。     e.g. python scriptname.py -f 'hello' --directory-p...
    99+
    2023-01-31
    模块 python getopt
  • Python getopt模块函数用法小
    官方模块说明:https://docs.python.org/2/library/getopt.html#module-getopt     shell中几乎所有的命令输入的时候都可以携带合适的参数来扩展其功能,例如:ls -l,cp -f...
    99+
    2023-01-31
    函数 模块 Python
  • getopt在Python中的用法
    这篇文章主要介绍“getopt在Python中的用法”,在日常操作中,相信很多人在getopt在Python中的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”getopt在Python中的用法”的疑惑有所...
    99+
    2023-06-04
  • Python中的sys模块、random模块和math模块
    一、sys运行时环境模块 sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境。 用法: sys.argv:命令行参数List,第...
    99+
    2024-04-02
  • Python中的time模块和calendar模块
    目录1、时间戳2、时间元组3、获取当前时间4、格式化时间5、格式化日期6、获取CPU时间7、日历模块在Python中对时间和日期的处理方式有很多,其中转换日期是最常见的一个功能。Py...
    99+
    2024-04-02
  • python中的模块
    模块:随着程序变的越来越大  为了便于维护 需要把它分为多个文件 为此python允许把定义放入一个文件 然后在其他脚本中将其作为模块导入 创建模块: 将相关的语句和定义放入与模块同名的文件中 #file:module.py def num...
    99+
    2023-01-31
    模块 python
  • python中的sys模块和os模块
    目录1.sys模块2.os模块(和操作系统相关数据)1.sys模块 sys模块的常见函数列表: sys.argv: 实现从程序外部向程序传递参数。sys.exit([arg]): 程...
    99+
    2024-04-02
  • python中的deque模块(collections的deque模块)
    目录 1. deque是python的collections中的一个类 2.deque的简单使用以及它的方法 2.1 创建deque的方法  2.2 创建deque时,并指定大小maxlen,即能装几个元素, 以及d...
    99+
    2023-09-25
    python 开发语言 collections deque 队列
  • Python中模块
    模块对我来说是什么        模块对我来说,感觉就像亲属或者朋友已经走过的路,他们已经趟过的浑水、掉过的坑、践行过的路线,全部提供给你,在你需要的时候请求帮助,借鉴他们的解决方法一样。都是为了方便走好人生路,用最短的路程走到成功的终...
    99+
    2023-01-31
    模块 Python
  • Python中的Subprocess模块
    原文出处:https://segmentfault.com/a/1190000009176351以前我一直用os.system()处理一些系统管理任务,因为我认为那是运行linux命令最简单的方式.我们能从Python官方文档里读到应该用s...
    99+
    2023-01-31
    模块 Python Subprocess
  • Python中的mmap模块
    mmap是一种虚拟内存映射文件的方法,即可以将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址和进程虚拟地址空间中一段虚拟地址的一一对映关系。 普通文件被映射到虚拟地址空间后,程序可以像操作内存一样操作文件,可以提高访问效率,...
    99+
    2023-01-31
    模块 Python mmap
  • Python中的 optparse模块
    python的内置模块中对于命令行的解析模块共两个getopt 和 optparse 。不过getopt过于简单,往往不能满足需求。此时可以使用optparse模块。这个模块相对于getopt更新,功能更强大。那么如何使用optparse模...
    99+
    2023-01-31
    模块 Python optparse
  • Python 中的 docx 模块
    Python 中的 docx 模块 本文介绍了 Python 中的 docx 模块,该模块可以用来创建、修改和读取 Microsoft Word 文档(.docx 文件)。本文包括以下内容: 什么是 ...
    99+
    2023-09-13
    python word
  • Python 中的 socket 模块
    本文参考PYTHON 网络编程 第一章import sockethelp(socket)    Functions:    socket() -- create a new socket object    socketpair() -- ...
    99+
    2023-01-31
    模块 Python socket
  • Python 中的 urllib2 模块
    通过python 的 urllib2 模块,可以轻易的去模拟用户访问网页的行为。这里将自己的学习过程简单的记录下来。一、urlopen函数    urlopen(url, data=None) -- Basic usage is the s...
    99+
    2023-01-31
    模块 Python
  • python中的wx模块
    wx包中的方法都是以大写字母开头的,而这和Python的习惯是相反的。原文位置:http://www.cnblogs.com/fnng/archive/2013/05/23/3094033.html---------------------...
    99+
    2023-01-31
    模块 python wx
  • Python 中 的 json 模块
    python 中的json 模板主要的两个功能:序列化和反序列化序列化: encoding   将python 数据 编码成json 字符串对应的函数有 dump 和 dumps反序列化: decoding  将json 字符串 解码成 p...
    99+
    2023-01-31
    模块 Python json
  • Python 中 os.path 模块的
    官网文档链接:   https://docs.python.org/3/library/os.path.html 概念:   该模块在路径名上实现了一些有用的功能,主要用于文件的属性获取 代码实现: os.path.abspath(pat...
    99+
    2023-01-30
    模块 Python os
  • Python 中的 pdb 模块
    PYTHON 代码,尤其是别人写的代码看不懂。怎么办? 其实PYTHON中也提供了类似于C语言中用于debug 的 gdb。它叫做pdb。结合本人自己的学习,进行简单的举例,以做备忘和补偿学习。首先参考资料:1、http://web.sta...
    99+
    2023-01-31
    模块 Python pdb
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作