1

I am working on a script that takes a text file containing IP addresses (one per line) and then passes each IP into a non-Python program command.

The result is an error:

TypeError: not all arguments converted during string formatting

import subprocess
list='c:\cmc_list.txt'
with open(list,'r') as cmc_list:
for i in cmc_list:
 racadm_command = "racadm -r %s -u root -p calvin getslotname" % i
output = subprocess.Popen(racadm_command % i, stdout = subprocess.PIPE, 
 shell=True).communicate()[0]
print(racadm_command, output)
OneCricketeer
193k20 gold badges146 silver badges276 bronze badges
asked Aug 9, 2018 at 0:45
7
  • 5
    Welcome to SO! Please be sure to post properly indented code, especially for python questions, where indentation matters. Commented Aug 9, 2018 at 0:47
  • 1
    What are the values in the text file? Has the file run properly in the past? Were there any edits since? Commented Aug 9, 2018 at 0:48
  • 1
    What is racadm_command % i supposed to do that isn't already done? Commented Aug 9, 2018 at 0:48
  • I'm with @cricket_007. I don't think the extra % i is needed after racadm_command. Commented Aug 9, 2018 at 0:54
  • By the way, you should avoid naming variables as list or any other Python data type Commented Aug 9, 2018 at 0:55

2 Answers 2

1

The string passed to the Popen command has already been formatted, so it has no % left to consume the i. Take away the "% i" and I think you'll be fine.

answered Aug 9, 2018 at 0:59
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Removing the % i from Popen allowed the script to run.
0

I believe you meant to do the formatting only once

import subprocess
cmc_list='c:\cmc_list.txt'
racadm_command_template = "racadm -r {} -u root -p calvin getslotname"
with open(cmc_list,'r') as f:
 for ip in f:
 cmd = racadm_command_template.format(ip)
 output = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell=True).communicate()
 print(cmd, output[0])

I also suggest using getpass for your password prompt, or import from an environment variable. Additionally, don't print out the password and please change it from the default

answered Aug 9, 2018 at 1:00

5 Comments

Thank you. Your version of the script runs successfully. This is my first python script. Its purely educational so password handling was not a priority but I do appreciate the tip. I'm assuming you are familiar with the targets here since you know its the default password. I'm curious about your choices to replace cmc_list with f and i with ip; also moving the [0] from the output definition to the print function.Why those choices?
Upon further inspection. For every IP in the list with the exception of the last it executes the command prematurely; so what should be "racadm -r %s -u root -p calvin getslotname" winds up being "racadm -r %s" on all but last IP in list. Thoughts?
i is commonly used as a numeric iterator, not a line in a file, while f is a "file object". Variable names aren't too important otherwise, but list is a builtin Python type, so you wouldn't have been able to use the list() function otherwise. I moved the [0] so you could also access the other values of the output, if needed. Regarding the final line, I'm not using %s here, but I don't think that should happen
please see above
Please post a new question rather than edit one you already accepted.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.