0

the below code shows attribute error when run the below code. import subprocess import sys import shlex

cmd = 'mtr -nrc30 -s1400 -z'
cmd = shlex.split(cmd)
abc = shlex.split(sys.argv[1])
subprocess.call([cmd,abc])

the error i get is below. What could the possible reason for this, when both the inputs to subprocess are converted to list

AttributeError: 'list' object has no attribute 'rfind'

7
  • Show us the full error traceback. Commented May 1, 2019 at 2:29
  • Specifically, which line of your code is the error coming from? Commented May 1, 2019 at 2:31
  • Line 12 from the code, which is the line where subprocess is called out Commented May 1, 2019 at 2:32
  • shlex.split() returns a list. So your argument to subprocess.call() is a list-of-lists, which is the wrong type. Commented May 1, 2019 at 2:33
  • 1
    @GreenCloakGuy abc is supposed to be an Ip and yes the solution did work out to be proper. Thanx for the help Commented May 1, 2019 at 2:39

1 Answer 1

1

For the sake of helping others with the same question: the issue here is this:

subprocess.call([cmd, abc])

where abc is a list of arguments you want to give the program specified in cmd, which were given to your program through sys.argv. What your current code ends up unpacking to, then, is the following:

# assume abc == ['arg1', 'arg2', ...]
subprocess.call([['mtr', '-nrc30', '-s1400', '-z'], ['arg1', 'arg2', ...]])

This isn't working, because it's a nested list. Subprocess only takes strings or a list of strings - so, to make it work, what you really want is

subprocess.call(['mtr', '-nrc30', '-s1400', '-z', 'arg1', 'arg2', ...])

This can be obtained by simply concatenating the two lists using +:

subprocess.call(cmd + abc)
Sign up to request clarification or add additional context in comments.

1 Comment

Really appreciat it. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.