如何传递参数sftp.get.获取在python中

2024-04-26 00:22:25 发布

您现在位置:Python中文网/ 问答频道 /正文

a = raw_input("Select your project: ")
stdin, stdout, stderr = ssh.exec_command('cd test \n cd software \n cd {0} \n ls'.format(a))
softwares = stdout.readlines()

我得到了这个的输出。但是,当我试图得到下面几行代码的输出时

^{pr2}$

我打了以下错误:

Traceback (most recent call last): File "D:\Python scripts for writing testcase\Paramiko.py", line 32, in sftp.get('{1}/{2}'.format(pwd1,b),'{2}'.format(b)) IndexError: tuple index out of range

我需要如何传递参数,因为路径会因不同的选择而改变。在


Tags: testprojectformatinputyourrawstderrstdin
2条回答

格式字符串中的数字应从0开始,而不是从1开始。在

>>> '{1}'.format(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> '{0}'.format(1)
'1'
>>> '{}'.format(1)  # auto numbering
'1'

Format string syntax。在

如果您使用的是python2.7+,那么可以完全忽略这个数字。(自动编号)。第二种格式可以是b(根本不需要使用格式,因为b已经是一个字符串对象)

^{pr2}$

'{2}'.format(b)

告诉format插入第二个参数,但只提供一个参数。更改为{1}。在

相关问题 更多 >