4

I was looking at this question.

In my case, I want to do a :

import subprocess
p = subprocess.Popen(['ls', 'folder/*.txt'], stdout=subprocess.PIPE, 
                                 stderr=subprocess.PIPE)

out, err = p.communicate()

Now I can check on the commandline that doing a "ls folder/*.txt" works, as the folder has many .txt files.

But in Python (2.6) I get:

ls: cannot access * : No such file or directory

I have tried doing: r'folder/\*.txt' r"folder/\*.txt" r'folder/\\*.txt' and other variations, but it seems Popen does not like the * character at all.

Is there any other way to escape *?

Community
  • 1
  • 1
aaa
  • 43
  • 1
  • 1
  • 3

2 Answers2

10

*.txt is expanded by your shell into file1.txt file2.txt ... automatically. If you quote *.txt, it doesn't work:

[~] ls "*.py"                                                                  
ls: cannot access *.py: No such file or directory
[~] ls *.py                                                                    
file1.py  file2.py file3.py

If you want to get files that match your pattern, use glob:

>>> import glob
>>> glob.glob('/etc/r*.conf')
['/etc/request-key.conf', '/etc/resolv.conf', '/etc/rc.conf']
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Thank you. When using Popen in Python, you have to use quotes. Please see the linked answer, or the Python documentation. – aaa Dec 14 '12 at 09:54
  • @aaa: The problem was that you weren't using a shell, which won't allow globbing. Calling `ls` with `Popen` isn't a great idea, IMO, since Python provides better tools for that. – Blender Dec 14 '12 at 10:08
  • ahhh, now I understand. Yes, you are right, glob is a much better library. See, I just learned a new thing :) – aaa Dec 14 '12 at 12:09
8

You can pass the parameter shell to True. It will allow globbing.

import subprocess
p = subprocess.Popen('ls folder/*.txt',
                     shell=True,
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.PIPE)
out, err = p.communicate()
Thomas Ruiz
  • 3,611
  • 2
  • 20
  • 33
  • 1
    Yes, this works. I had incorrectly assumed that the args to Popen must be arrayed, as that's how all examples do it – aaa Dec 14 '12 at 09:58
  • p.communicate() is a blocking call , can we have the same behavior in a non-blocking manner ? thanks – Anurag Dec 14 '12 at 12:52