I am trying to run a shell command from within a python script. This is run within a CentOS linux distro. Python version is 2.6.6.
Filesystem is as follows:
bash-4.1$ ls
example.py file1 file2 file3
Script example.py is as follows:
#!/usr/bin/python
import os
import logging
import subprocess
import shlex
if __name__ == "__main__":
path = os.getcwd()
command = "touch file1 file2 file3"
print("Running \"{0}\" at {1}".format(command, path))
command_as_list = shlex.split(command)
print("command_as_list = {0}".format(command_as_list))
process = subprocess.Popen(command_as_list,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=path)
if process.wait() == -1:
raise Exception("Error in command {0}".format(command))
if process.stderr is not None:
print("Error ouput: {0}".format(process.stderr.read()))
Running the script yields following output:
bash-4.1$ ./example.py
Running "touch file1 file2 file3" at /home/example
command_as_list = ['touch', 'file1', 'file2', 'file3']
Error ouput: touch: missing file operand
Try `touch --help' for more information
Running the command directly on the command line works as expected:
bash-4.1$ touch file1 file2 file3
It looks like the file1 file2 file3
part is being ignored. Any idea why? Am I missing something related to the use of subprocess.Open()
?