相关疑难解决方法(0)

sys.argv [1]在脚本中的含义

我现在正在教自己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()
Run Code Online (Sandbox Code Playgroud)

python

135
推荐指数
6
解决办法
37万
查看次数

sys.argv [1],IndexError:列表索引超出范围

我遇到了以下Python代码部分的问题:

# Open/Create the output file
with open(sys.argv[1] + '/Concatenated.csv', 'w+') as outfile:

    try:
        with open(sys.argv[1] + '/MatrixHeader.csv') as headerfile:
            for line in headerfile:
                outfile.write(line + '\n')
    except:
        print 'No Header File'
Run Code Online (Sandbox Code Playgroud)

具体来说错误如下:

Traceback (most recent call last): File "ConcatenateFiles.py", line 12, in <module> with open(sys.argv[1] + 'Concatenated.csv', 'w+') as outfile:
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

我做了一些研究,似乎sys.argv在运行脚本时可能需要在命令行进行参数,但我不确定要添加什么或问题可能是什么!我也搜索了网站,但我发现的所有解决方案都没有评论和/或不包括我的开放功能.

任何帮助是极大的赞赏.

python

10
推荐指数
3
解决办法
7万
查看次数

标签 统计

python ×2