Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess Popen: Why does "ls *.txt" not work? [duplicate]

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

like image 685
aaa Avatar asked Dec 03 '22 00:12

aaa


1 Answers

*.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']
like image 166
Blender Avatar answered Dec 22 '22 11:12

Blender