Reputation: 1686
proc = subprocess.Popen(['ls', '-v', self.localDbPath+'labris.urls.*'], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line != '':
print line
else:
break
When using the above code I get the error saying:
ls: /var/lib/labrisDB/labris.urls.*: No such file or directory
But when I dıo the same from shell I get no errors:
ls -v /var/lib/labrisDB/labris.urls.*
Also this doesn't give any error either:
proc = subprocess.Popen(['ls', '-v', self.localDbPath], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line != '':
print line
else:
break
Why is the first code failing? What am I missing?
Upvotes: 2
Views: 3875
Reputation: 1019
You get error because python subprocess could not be able to expand * like bash.
Change your code like that:
from glob import glob
proc = subprocess.Popen(['ls', '-v'] + glob(self.localDbPath+'labris.urls.*'), stdout=subprocess.PIPE)
Here is more information about glob expansion in python and solutions: Shell expansion in Python subprocess
Upvotes: 5
Reputation: 7717
Globbing is done by the shell. So when you're running ls *
in a terminal, your shell is actually calling ls file1 file2 file3 ...
.
If you want to do something similar, you should have a look at the glob
module, or just run your command through a shell:
proc = subprocess.Popen('ls -v ' + self.localDbPath + 'labris.urls.*',
shell=True,
stdout=subprocess.PIPE)
(If you choose the latter, be sure to read the security warnings!)
Upvotes: 1