sys.argv[1] 在脚本中的含义 [英] sys.argv[1] meaning in script

查看:62
本文介绍了sys.argv[1] 在脚本中的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在自学 Python,只是想知道(参考下面我的示例)用简化的术语表示 sys.argv[1] 代表什么.是否只是要求输入?

I'm currently teaching myself Python and was just wondering (In reference to my example below) in simplified terms what the sys.argv[1] represents. Is it simply asking for an input?

#!/usr/bin/python3.1

# import modules used here -- sys is a very standard one
import sys

# Gather our code in a main() function
def main():
  print ('Hello there', sys.argv[1])
  # Command line args are in sys.argv[1], sys.argv[2] ..
  # sys.argv[0] is the script name itself and can be ignored

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  main()

推荐答案

我想指出,之前的答案对用户的知识做了很多假设.这个答案试图在更教程的层面上回答这个问题.

对于 Python 的每次调用,sys.argv 自动是一个字符串列表,表示命令行上的参数(以空格分隔).该名称来自 C 编程约定,其中 argv 和 argc代表命令行参数.

For every invocation of Python, sys.argv is automatically a list of strings representing the arguments (as separated by spaces) on the command-line. The name comes from the C programming convention in which argv and argc represent the command line arguments.

当您熟悉 Python 时,您会想要了解有关列表和字符串的更多信息,但与此同时,您需要了解以下几点.

You'll want to learn more about lists and strings as you're familiarizing yourself with Python, but in the meantime, here are a few things to know.

您可以简单地创建一个脚本来打印所表示的参数.它还使用列表中的 len 函数打印参数的数量.

You can simply create a script that prints the arguments as they're represented. It also prints the number of arguments, using the len function on the list.

from __future__ import print_function
import sys
print(sys.argv, len(sys.argv))

脚本需要 Python 2.6 或更高版本.如果你调用这个脚本 print_args.py,你可以用不同的参数调用它,看看会发生什么.

The script requires Python 2.6 or later. If you call this script print_args.py, you can invoke it with different arguments to see what happens.

> python print_args.py
['print_args.py'] 1

> python print_args.py foo and bar
['print_args.py', 'foo', 'and', 'bar'] 4

> python print_args.py "foo and bar"
['print_args.py', 'foo and bar'] 2

> python print_args.py "foo and bar" and baz
['print_args.py', 'foo and bar', 'and', 'baz'] 4

如您所见,命令行参数包括脚本名称但不包括解释器名称.从这个意义上说,Python 将脚本视为可执行文件.如果您需要知道可执行文件的名称(在本例中为 python),您可以使用 sys.executable.

As you can see, the command-line arguments include the script name but not the interpreter name. In this sense, Python treats the script as the executable. If you need to know the name of the executable (python in this case), you can use sys.executable.

您可以从示例中看到,如果用户调用脚本时将参数封装在引号中,则可能会收到包含空格的参数,因此您得到的是用户提供的参数列表.

You can see from the examples that it is possible to receive arguments that do contain spaces if the user invoked the script with arguments encapsulated in quotes, so what you get is the list of arguments as supplied by the user.

现在在您的 Python 代码中,您可以将此字符串列表用作程序的输入.由于列表由从零开始的整数索引,您可以使用 list[0] 语法获取单个项目.例如获取脚本名称:

Now in your Python code, you can use this list of strings as input to your program. Since lists are indexed by zero-based integers, you can get the individual items using the list[0] syntax. For example, to get the script name:

script_name = sys.argv[0] # this will always work.

虽然有趣,但您很少需要知道您的脚本名称.要获取文件名脚本后的第一个参数,您可以执行以下操作:

Although interesting, you rarely need to know your script name. To get the first argument after the script for a filename, you could do the following:

filename = sys.argv[1]

这是一个非常常见的用法,但请注意,如果没有提供参数,它将失败并返回 IndexError.

This is a very common usage, but note that it will fail with an IndexError if no argument was supplied.

此外,Python 允许您引用列表的一部分,因此要获取另一个列表,仅包含用户提供的参数(但没有脚本名称),您可以这样做

Also, Python lets you reference a slice of a list, so to get another list of just the user-supplied arguments (but without the script name), you can do

user_args = sys.argv[1:] # get everything after the script name

此外,Python 允许您将一系列项目(包括列表)分配给变量名称.因此,如果您希望用户始终提供两个参数,则可以将这些参数(作为字符串)分配给两个变量:

Additionally, Python allows you to assign a sequence of items (including lists) to variable names. So if you expect the user to always supply two arguments, you can assign those arguments (as strings) to two variables:

user_args = sys.argv[1:]
fun, games = user_args # len(user_args) had better be 2

因此,为了回答您的具体问题,sys.argv[1] 表示提供给相关脚本的第一个命令行参数(作为 string).它不会提示输入,但如果在脚本名称后面的命令行上没有提供参数,它将失败并显示 IndexError.

So, to answer your specific question, sys.argv[1] represents the first command-line argument (as a string) supplied to the script in question. It will not prompt for input, but it will fail with an IndexError if no arguments are supplied on the command-line following the script name.

这篇关于sys.argv[1] 在脚本中的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆