Search code examples
pythonsubprocessls

Using ls in Python subprocess.Popen function


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?


Solution

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