zoukankan      html  css  js  c++  java
  • Python sys.argv[]用法

    sys.argv,其实就是一个list,它是sys模块下的一个全局变量,第一个元素是模块名、后面是依次传入的参数

    比如可以这样传入 pyton temp.py a b c d,一共传入a、b、c、d四个参数

    len(sys.argv) == 5

    那么sys.argv[0]  == "temp.py"   sys.argv[1] == "a"    sys.argv[2] == "b"

    看一下python代码:

    在看下面代码,进一步进行了解

    原来sys模块sys.argv是个列表,而sys.argv[0]、sys.argv[1]、sys.argv[2]只不过是在取列表中的值,但这个列表有我们决定。

    =============================================================================================================

    =============================================================================================================

    Sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始,以下两个例子说明:

     1、使用sys.argv[]的一简单实例

    import sys,os  
    os.system(sys.argv[1])  

    这个例子os.system接收命令行参数,运行参数指令,保存为sample1.py,命令行带参数运行sample1.py notepad,将打开记事本程序。

    2、这个例子是简明python教程上的,明白它之后你就明白sys.argv[]了

    import sys  
    def readfile(filename):  #从文件中读出文件内容  
        '''''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  
    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.我们验证一下:

    1)       命令行带参数运行:sample.py –version  输出结果为:version 1.2

    2)       命令行带参数运行:sample.py –help  输出结果为:This program prints files……

    3)       在与sample.py同一目录下,新建a.txt的记事本文件,内容为:test argv;命令行带参数运行:sample.py a.txt,输出结果为a.txt文件内容:test argv,这里也可以多带几个参数,程序会先后输出参数文件内容。

    我的问题是:为何[]里会有”:“冒号?sys.argv[1:]又代表什么?
    
    这就是个分片操作
    简单例子
    l = [1,2,3,4,5]
    l[1:],意思就是取列表l第一个元素后的值,也就是[2,3,4,5]
    l[2:], -----------------[3,4,5]
    还可以倒序来取
    l[:-1],-----------------从右到左,取最后一个元素前面的值,[1,2,3,4]
    l[:-2],-----------------[1,2,3]
    而你问的sys.argv[1:],这个是你main(xxx)方法的参数,那么这个参数从哪里来
    
    从这里来,比如
    python test.py --t help --v
    那么sys.argv就是['test.py', '--t', 'help', '--v']
    那么sys.argv[1:]就是['--t', 'help', '--v']
  • 相关阅读:
    node.js 安装后怎么打开 node.js 命令框
    thinkPHP5 where多条件查询
    网站title中的图标
    第一次写博客
    Solution to copy paste not working in Remote Desktop
    The operation could not be completed. (Microsoft.Dynamics.BusinessConnectorNet)
    The package failed to load due to error 0xC0011008
    VS2013常用快捷键
    微软Dynamics AX的三层架构
    怎样在TFS(Team Foundation Server)中链接团队项目
  • 原文地址:https://www.cnblogs.com/111testing/p/10480531.html
Copyright © 2011-2022 走看看