I’m a learning newbie to python and to working in the command line, e.g. piping.
I’ve read that subprocess is encouraged way instead of os.system. I’m creating a script which invokes the shell and I have not been able to do it using subprocess. Using os.system was a snap though:
os.system("cut -f1-4 " + temp1.name + "| uniq --count | sort -rn > " + temp2.name)
I’ve used subprocess with success for other commands, but not those that combine more than one tools with “|”. Reading the subprocess python documentation was confusing and not helpful to me. I’ve also tried searching other questions but could not find something similar to my problem. This is what I’ve tried (and failed):
subprocess.call = (["cut", "-f1-4", temp1.name, "|", "uniq", "--count", "|", "sort". "-rn"], stdout = open(temp2.name, 'w'))
I’ve also tried substituting sp.call with sp.Popen, but failed. Can anyone please help with some clear examples and explanation?
Thanks!
If you want to use pipes you should add
shell=TruePlease note that if
temp1.nameortemp2.namecome from an untrusted source (e.g. from data supplied by a user in a web application) usingshell=Trueis be a security risk.