I am new to the subprocess.call function and I have tried different combinations of the same call but it is not working.
I am trying to execute the following command:
cmd = 'sort -k1,1 -k4,4n -k5,5n '+outpath+fnametempout+' > '+outpath+fnameout
print cmd
If I try the call I get an error:
cmd = cmd.split(" ")
print cmd
subprocess.call(cmd)
the error I get is:
sort: stat failed: >: No such file or directory
Doing it this way, you need
shell=Trueto allow the shell redirection to work.A better way is:
which avoids spawning a shell all-together and is safe from shell injection type attacks. Here,
cmdis a list as in your original, e.g.It should also be stated that python has really nice sorting facilities and so I doubt that it is actually necessary to pass the job off to
sortvia a subprocess.Finally, rather than using
str.splitto split the arguments, from a string, it’s probably better to useshlex.splitas that will properly handle quoted strings.