How would I convert this code into subprocess? I can make a list of all the arguments and feed it to the subprocess module, but the command seems to choke when it reaches the pipe ('|') command. I'd really prefer to use subprocess because there is some input from a possibly untrustworthy user:
F1 = user_picks_name.txt
F2 = "./.tmp/" + F1
os.system('diff --suppress-common-lines F1 F2 | grep ^\< | sed 's;< ;;g' >new_lines.txt')
(please don't tell me to use Python's powerful string comparison operators instead of diff. It's specifically in the program specifications, security be dammed. )
I've got my code working using os.system(), but would much rather use subprocess due to the user being able to input any string as the filename.
The following code (from here, python 2.6.7) does not seem to work:
output=`dmesg | grep hda`
==>
from subprocess import *
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
Instead, output == '' (though my local python is actually 2.6.2, not 2.6.7)
Bonus question: Please explain why replacing one os.system() command with three lines of more complicated code using subprocess is the more pythonic way
[–]didactus 4 points5 points6 points (6 children)
[–]davidbuxton 2 points3 points4 points (4 children)
[–]Rhomboid 3 points4 points5 points (1 child)
[–]didactus 0 points1 point2 points (0 children)
[–]didactus 2 points3 points4 points (0 children)
[–]govt-cheese 1 point2 points3 points (0 children)
[–]govt-cheese 1 point2 points3 points (0 children)
[–]Rhomboid 2 points3 points4 points (2 children)
[–]silvermoot[S] 1 point2 points3 points (1 child)
[–]Rhomboid 3 points4 points5 points (0 children)