0

learning python to replace bash here and I've tried to generate an argument and pass it to subprocess.call. Tried a couple of ways but it only ever seems to run the first section. as an example:

#!/usr/bin/python
import subprocess
command='ls -l'
dibner='scum.py'
commands=[command, dibner]
subprocess.call(commands,shell=True)

logically I thought this would call ls -l scum.py but it seems to just call ls -l. Any idea what I'm doing wrong here?

1 Answer 1

2

You need to pass ls, -l separately.

...
command = ['ls', '-l']
dibner = 'scum.py'
commands = comamnd + [dibner]
...

Otherwise, ls -l instead of ls is interpreted as a command.

Sign up to request clarification or add additional context in comments.

7 Comments

I tried this which is pretty much what you said:#!/usr/bin/python import subprocess command=['ls', '-l'] dibner='scum.py' commands=command + [dibner] subprocess.call(commands,shell=True)
@KeefBaker, What do you mean is only ran ls ?
@KeefBaker, Try to remove shell=True.
I also managed to get round it using #!/usr/bin/python import subprocess dibner = 'scum.py' command = "ls -l %s" % dibner subprocess.call(command,shell=True)
@KeefBaker, Instead of using string formatting, pass arguments as a list items and use shell=False (not shell=True)
|

Your Answer

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.