Tag: linux
python commands.getouput vs subprocess.Popen
by admin on Jul.02, 2009, under Uncategorized
I have been using the commands module for quite some time, purely because it is simple to use.
Now that i have to go to writing code to work on windows, its time to start using the suproccess module. Also to make it easier for the other hard core programmers i will be working with to read my code.
Using the commands module to get the output of a command:
</code> import commands output = commands.getoutput('ls -lah /home/') outputList = output.split('\n') print outputList ["kym.watts","guest"] <pre><code>
The command that is run by Popen, needs to be supplied to the command as a list, no spaces.
Using subproccess command:
</code> <pre class="c:firstline[1]">import subprocess output = subprocess.Popen(["ls","-lah","/home/"], stdout=subprocess.PIPE).communicate()[0] outputList = output.split('\n') print outputList ["kym.watts","guest"]</pre> <pre><code>
Im pretty sure that in a few months time i will have forgotten about this way of working.