我目前正在自学 Python,我只是想知道(参考下面的示例) sys.argv[1] 用简单的术语代表什么。它只是要求输入吗?#!/usr/bin/pyt...
我目前正在自学 Python,我只是想知道(参考下面的示例)用简单的术语来说,这 sys.argv[1]
代表什么。它只是要求输入吗?
#!/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()
您可能被引导到这里,因为您询问的是使用sys.argv
的代码中的IndexError
。问题不在于您的代码;问题在于您需要以使sys.argv
包含正确值的方式运行程序。请阅读答案以了解sys.argv
工作原理。
If you have read and understood the answers, and are still having problems on Windows , check if Python 脚本是否不采用 Windows 中的 sys.argv 来 fixes the issue. If you are trying to run the program from inside an IDE , you may need IDE-specific help - please search, but first check if you can run the program successfully from the command line.