python中argv是什么_什么是“argv”,它做什么?

在浪费时间“开始学习更高层次的代码”之前,您需要学习如何找到这样的信息。知道如何查找一个不熟悉的函数/类/变量,而不必等待30-60分钟让其他人来回答你(显然,在这个过程中会获得多个下一轮投票),这比在你的曲目中再添加一条信息有用得多。

从内置帮助:>>> import sys

>>> help(sys)

argv -- command line arguments; argv[0] is the script pathname if known

这适用于任何模块。通常,模块中的各个类和函数都有更详细的信息(但这对argv不起作用,因为它只是一个list,列表没有自定义帮助)。sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.

To loop over the standard input, or the list of files given on the command line, see the fileinput module.

搜索“python argv”的第一个web结果是a blog post by Python author Guido,这可能对您来说有点太高级了,但第二个结果是a tutorial关于“命令行参数”。

正如大多数这些事情告诉您的,许多简单的脚本通常只使用fileinput,而不是直接处理argv,而更复杂的脚本通常需要使用argparse(或者optparse,getopt,或者其他替代方法,如果您需要使用Python 2.6或更早版本)。

有一个例子sys.argv是正确的,它是一个将相对路径名转换为绝对路径名的简单程序(对于那些不在linux或其他带有abspath、GNU readlink或类似内置工具的平台上的用户):import os, sys

print('\n'.join(os.path.abspath(arg) for arg in sys.argv[1:]))

或者这个“add”工具只是添加了一堆数字(对于那些卡在cmd.exe或其他没有内置算术的有缺陷的shell):import sys

print(sum(int(arg) for arg in sys.argv[1:]))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值