2017-04-10 4 views
0
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() 

私はこのための出力を得ました。しかし、私はsftp.getの引数をPythonで渡す方法

stdin, stdout, stderr = ssh.exec_command('cd test \n cd software \n cd {0} \n pwd'.format(a)) 
pwd = stdout.readlines() 
pwd1 = '\n'.join(pwd) 
print pwd1 

b = raw_input("Select the software you want to download: ") 

sftp = ssh.open_sftp() 
sftp.get('{1}/{2}'.format(pwd1,b),'{2}'.format(b)) 

、コードの以下の行のための出力を取得しようとしていたとき、私は以下のエラーで打っています:

私はパスとして、そこに引数を渡す必要がどのように

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

異なる選択のために変更されます。フォーマット文字列で

答えて

2

番号がない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' 

から、0から開始しなければならないFormat string syntaxを参照してください。

python 2.7+を使用している場合は、番号を完全に省略することができます。 (自動ナンバリング)。そして、第二のフォーマットは、あなたが二番目の引数を挿入し、まだ一つだけを提供してフォーマットを伝えるだけでb(全くの形式を使用する必要はありません、bはすでに文字列オブジェクトであるため)

sftp.get('{}/{}'.format(pwd1, b), b) 
0

'{2}'.format(b)

することができます。 {1}に変更してください。

関連する問題