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

允我心安 提交于 2019-11-26 18:35:16

问题


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


来源:https://stackoverflow.com/questions/13875978/python-subprocess-popen-why-does-ls-txt-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!