2
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?

jamylak
  • 128,818
  • 30
  • 231
  • 230
Alptugay
  • 1,676
  • 4
  • 22
  • 29
  • maybe using like this will solve http://stackoverflow.com/questions/9997048/python-subprocess-wildcard-usage – denizeren Apr 12 '13 at 07:00

2 Answers2

5

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

Community
  • 1
  • 1
Mustafa Simav
  • 1,019
  • 5
  • 6
1

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!)

Schnouki
  • 7,527
  • 3
  • 33
  • 38