学习Python 3.4 - 使用argv

时间:2015-06-22 22:49:09

标签: python

我正在努力学习“艰难学习Python”。我完全被“练习13:参数,解包,变量”所困扰。我在Windows 7环境中使用Sublime Text 3.

这是我的代码:

from sys import argv
script, first, second = argv
print ("The script is called:", script)
print ("Your first variable:", first)
print ("Your second variable:", second)

现在我的问题:

  1. 我是否将它放在脚本文件夹中,然后从另一个.py文件中引用它... ...

    c:\scripts\python\ex11.py one_thing second_thing another_thing
    

    ...或者我在我的脚本文件夹中使用一个文件来引用我的文件放在另一个持有.py文件的文件夹中?

  2. 指向另一个文件夹中另一个文件的语法是什么?

2 个答案:

答案 0 :(得分:1)

有助于确定arguments vector, or argv实际上做了什么。它会从命令行中读取您传入的内容。

所以,这意味着这个命令应该可以工作(前提是Python在你的路径中,你可以用这种方式执行文件):

C:\scripts\python\ex11.py one_thing second_thing another_thing

请注意,您只会看到one_thing和second_thing出现;第一个值是脚本的名称。

答案 1 :(得分:0)

使用命令行运行程序时,可以指定参数以使程序以不同方式运行。

在此示例中,您需要打开PowerShell,运行cd c:\scripts\python\,然后python ex11.py one_thing second_thing another_thing