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

*.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']

Leave a Comment