相关疑难解决方法(0)

了解切片表示法

我需要在Python的切片表示法上有一个很好的解释(引用是一个加号).

对我来说,这种符号需要一点点提升.

它看起来非常强大,但我还没有完全了解它.

python iterable list slice

3024
推荐指数
33
解决办法
159万
查看次数


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脚本不在Windows中使用sys.argv

我有两台带有Windows的计算机,我刚刚在其中一台计算机上发现,如果我直接运行python代码,例如:

test_args.py input1 input2
Run Code Online (Sandbox Code Playgroud)

Python不会识别我给出的输入,但这有效:

python test_args.py input1 input2
Run Code Online (Sandbox Code Playgroud)

我试过这段代码:

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
Run Code Online (Sandbox Code Playgroud)

第一种方式(test_args.py)返回:

Number of arguments: 1 arguments.
Argument List: ['D:\\Test\\args\\test_args.py']
Run Code Online (Sandbox Code Playgroud)

虽然sceond方式(python test_args.py input1 input2)返回:

Number of arguments: 3 arguments.
Argument List: ['D:\\Test\\args\\test_args.py', 'input1', 'input2']
Run Code Online (Sandbox Code Playgroud)

关于这可能发生什么的任何想法?此问题仅发生在我的一台计算机上,两台都有相同版本的Windows.

谢谢!

解决了:

我在regedit关键字"python"中搜索,发现在"C:\ Python27\python.exe""%1"之后缺少%*的两个键:

电脑\ HKEY_CLASSES_ROOT \应用程序\ python.exe

电脑\ HKEY_CLASSES_ROOT\py_auto_file \壳\开放\命令

并且.py与py_auto_file相关联,即使我尝试了关联.py Python.File

更改两个键解决了这个问题,谢谢!

python windows cmd command-line-arguments

9
推荐指数
1
解决办法
4562
查看次数

什么是"argv",它有什么作用?

ARGV

这是什么地球!?

编辑:如果可以的话,请你写一两行并解释它是如何工作的?

python

8
推荐指数
4
解决办法
6万
查看次数

使用sys.argv [1]时"列表索引超出范围"

我正在编写一个简单的Python客户端和服务器,它可以很好地在我的代码中传递服务器地址,但是,我希望用户能够输入服务器地址并抛出错误,如果它不正确.当我有下面的代码时,我收到来自终端"列表索引超出范围"的错误消息.

server = (sys.argv[1])
serverAdd = (server, '65652') # server address and port number
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我这个.

当我在python中运行我的客户端程序时,我希望能够输入一个地址来连接并将其存储在服务器中.我通过输入programname.py直接从命令行运行程序.服务器已在运行侦听传入连接.

python command-line-arguments python-3.x

8
推荐指数
1
解决办法
3万
查看次数

通过 pytest 在 python 中传递命令行参数

我可以在运行时传递命令行参数

python <filename>.py arg1
Run Code Online (Sandbox Code Playgroud)

但是当我尝试传递运行 pytest 的命令行参数时,它失败并给出如下错误。你能给些建议么。

pytest <filename>.py arg1
ERROR: file not found: arg1
Run Code Online (Sandbox Code Playgroud)

编辑:

例如,我正在考虑以这种方式使用它,假设我已经传递了一个参数并且正在通过 sys.argv 读取它:

import sys
arg = sys.argv[3]
def f():
    return 3

def test_function():
    assert f() == arg
Run Code Online (Sandbox Code Playgroud)

python

8
推荐指数
2
解决办法
1万
查看次数

在Python中使用命令行参数:了解sys.argv

我目前正在学习Python的艰难之路.我想这个例子可能已经过时了,所以我想在这里得到反馈.

我正在使用Python 3.1

from sys import argv

script, first, second, third = argv

print("the script is called:", (script))
print("your first variable is:", (first))
print("your second variable is:", (second))
print("your third variable is:", (third))
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

Traceback (most recent call last):
  File "/path/ch13.py", line 3, in <module>
    script, first, second, third, bacon = argv
ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)

知道什么是错的吗?

python argv command-line-arguments

6
推荐指数
1
解决办法
2万
查看次数

python中的stdin和sys.argv有什么区别?

我在编码挑战中停靠点,指定我需要从STDIN读取.这是我的输入法:

def __init__(self, input):
    self._dictionary = {}
    with open(input, 'r') as f:
        reader = csv.reader(f, delimiter='\t')
        for row in reader:
          if self._dictionary.__contains__(row[0]):
            self._dictionary[row[0]].append(row[1])
          else:
            self._dictionary.update({row[0]: row[1].split()})
Run Code Online (Sandbox Code Playgroud)

并在脚本的末尾

if __name__ == "__main__":
    script = Script(sys.argv[1])
    for line in script.output_method():
      print line
Run Code Online (Sandbox Code Playgroud)

在要求从标准输入读取的挑战中使用sys.argv是错误的吗?有什么不同?我应该怎样做才能满足要求?

python

2
推荐指数
1
解决办法
1734
查看次数