Предположим, у меня есть очень простой файл Python C:\Scripts\helloscript.py
Код: Выделить всё
print "hello"
Код: Выделить всё
C:\>helloscript
Код: Выделить всё
hello
Код: Выделить всё
import sys
print "hello,", sys.argv[1]
Код: Выделить всё
c:\>helloscript brian
Traceback (most recent call last):
File "C:\Scripts\helloscript.py", line 2, in
print sys.argv[1]
IndexError: list index out of range
Код: Выделить всё
sys.argvКод: Выделить всё
['C:\\Scripts\\helloscript.py']
Если я вызываю сценарий явно, как обычно:
Код: Выделить всё
C:\>python C:\Scripts\helloscript.py brian
Код: Выделить всё
hello, brian
Код: Выделить всё
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-f", "--firstname", action="store", type="string", dest="firstname")
(options, args) = parser.parse_args()
print "hello,", options.firstname
Код: Выделить всё
hello, None
Вот в чем вопрос. Что происходит? Почему при неявном вызове моего сценария в sys.argv не заполняется больше, чем просто имя сценария?
Подробнее здесь: https://stackoverflow.com/questions/152 ... on-windows
Мобильная версия