简体   繁体   中英

Python not all arguments converted during string formatting?

I've received following error when trying to execute script to copy specific file from remote server to jumphost and then to my local computer.

Traceback (most recent call last):
  File "list_files.py", line 34, in <module>
    stdin, stdout, stderr = p.exec_command('sshpass -p %s scp -r hostname:/directory/' + list[file_number] + ' ' + '/desired_directory' % (password))
TypeError: not all arguments converted during string formatting
stdin, stdout, stdeer = p.exec_command('sshpass -p %s ssh hostname ls /directory/' % (password))
for line in stdout:
    if line:
        list.append(line)
i = 0
for x in list:
    print(str(i) + '. ' + x)
    i+=1

file_number = input("Type log file number you would like to get: ")

stdin, stdout, stderr = p.exec_command('sshpass -p %s scp -r hostname:/directory/' + list[file_number] + ' ' + '/desired_directory/downloaded_file_name' % (password))

I've already tried change syntax to the ${0} instead of %s but it didn't help. I assume problem is in list[file_number] not in %s.

'sshpass -p %s scp -r hostname:/directory/' + list[file_number] + ' ' + '/desired_directory/downloaded_file_name' % (password)

Because of operator precedence, this fragment means the formatting only applies to '/desired_directory/downloaded_file_name' . ;)

To fix it, either put all string concatenation in parentheses or move % formatting to the first element only:

('sshpass -p %s scp -r hostname:/directory/' + list[file_number] + ' ' + '/desired_directory/downloaded_file_name') % (password)```
'sshpass -p %s scp -r hostname:/directory/' % (password) + list[file_number] + ' ' + '/desired_directory/downloaded_file_name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM