美文网首页
[转]Python-sys.argv[]用法

[转]Python-sys.argv[]用法

作者: 大魔王是本人 | 来源:发表于2018-01-26 20:12 被阅读82次

Sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始。

例如:

import sys

defreadfile(filename):#从文件中读出文件内容

'''Print a file to the standard output.'''

f = file(filename)

whileTrue:

line = f.readline()

iflen(line) ==0:

break

printline,# notice comma  分别输出每行内容

f.close()

# Script starts from here

iflen(sys.argv) <2:

print'No action specified.'

sys.exit()

ifsys.argv[1].startswith('--'):

option = sys.argv[1][2:]

# fetch sys.argv[1] but without the first two characters

ifoption =='version':#当命令行参数为-- version,显示版本号

print'Version 1.2'

elifoption =='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:

forfilenameinsys.argv[1:]:#当参数为文件名时,传入readfile,读出其内容

readfile(filename)

相关文章

  • [转]Python-sys.argv[]用法

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

  • sort用法(转)

    一、c++标准库里的排序函数的使用方法 ​I)Sort函数包含在头文件为#include的c++标准库中 II)S...

  • qsort用法(转)

    用 法: void qsort(void *base, int nelem, int width, int(*fc...

  • [转]meshgrid用法

    详细解释:help meshgridmeshgrid用于从数组a和b产生网格。生成的网格矩阵A和B大小是相同的。它...

  • 【转】sscanf用法

    附上原地址:https://www.cnblogs.com/hejing-swust/p/7793958.html...

  • setpci用法 [转]

    setpci命令_Linux setpci 命令用法详解:查询和配置PCI设备的使用工具 (ailinux.net...

  • Python-sys.argv——notes

    Python sys.argv Test1 sys.argv-test.py文件,加入代码如下: shell运行如...

  • CGContext用法详解_转

    原文链接如下: http://blog.csdn.net/zhenyu5211314/article/detail...

  • Wireshark基本用法[转]

    如果看了这个你还是不会用Wireshark,那就去杀了她吧,地址EMC中文支持论坛https://communit...

  • CronTigger的用法【转】

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,...

网友评论

      本文标题:[转]Python-sys.argv[]用法

      本文链接:https://www.haomeiwen.com/subject/mwgwaxtx.html