sys.argv[1], IndexError: 列表索引超出范围 [英] sys.argv[1], IndexError: list index out of range

查看:114
本文介绍了sys.argv[1], IndexError: 列表索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having an issue with the following section of Python code:

# 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'

具体报错如下:

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

我做了一些研究,似乎 sys.argv 在运行脚本时可能需要在命令行中输入一个参数,但我不确定要添加什么或有什么问题可能!我也搜索过该网站,但我发现的所有解决方案都没有评论和/或不像我的那样包含 open 功能.

I've done some research and it seems that the sys.argv might require an argument at the command line when running the script, but I'm not sure what to add or what the issue might be! I've also searched the site, but all of the solutions I've found either have no comments and/or don't include the open function as mine does.

非常感谢任何帮助.

推荐答案

sys.argv 代表你用来执行脚本的命令行选项.

sys.argv represents the command line options you execute a script with.

sys.argv[0] 是您正在运行的脚本的名称.所有附加选项都包含在 sys.argv[1:] 中.

sys.argv[0] is the name of the script you are running. All additional options are contained in sys.argv[1:].

您正在尝试打开一个使用 sys.argv[1](第一个参数)作为目录的文件.

You are attempting to open a file that uses sys.argv[1] (the first argument) as what looks to be the directory.

尝试运行这样的程序:

python ConcatenateFiles.py /tmp

这篇关于sys.argv[1], IndexError: 列表索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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