Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

commands vs subprocess

Just wondering if anybody could tell me why

import subprocess, commands

p=subprocess.Popen(["ls", "*00080"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output=p.communicate()[0]
print "o", output
result=commands.getoutput("ls *00080")
print "o", result

gives the outputs:

o ls: cannot access *00080: No such file or directory

o 010.010.013.165.42974-010.010.013.164.00080

Both should find the file shouldn't they?

A

like image 699
amadain Avatar asked Dec 26 '22 17:12

amadain


1 Answers

commands spaws a shell which does the glob expansion. subprocess doesn't spawn a shell unless you pass shell = True.

In other words:

p=subprocess.Popen("ls *00080",shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

should do the same thing that commands did.

like image 191
mgilson Avatar answered Dec 29 '22 07:12

mgilson