Python subprocess Popen: Why does “ls *.txt” not w

2019-01-08 01:50发布

问题:

This question already has an answer here:

  • Python subprocess wildcard usage 2 answers

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

回答1:

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


回答2:

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