Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess wildcard usage

import os  import subprocess  proc = subprocess.Popen(['ls','*.bc'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)  out,err = proc.communicate()  print out 

This script should print all the files with .bc suffix however it returns an empty list. If I do ls *.bc manually in the command line it works. Doing ['ls','test.bc'] inside the script works as well but for some reason the star symbol doesnt work.. Any ideas ?

like image 451
Cemre Mengü Avatar asked Apr 03 '12 15:04

Cemre Mengü


1 Answers

You need to supply shell=True to execute the command through a shell interpreter. If you do that however, you can no longer supply a list as the first argument, because the arguments will get quoted then. Instead, specify the raw commandline as you want it to be passed to the shell:

 proc = subprocess.Popen('ls *.bc', shell=True,                                     stdout=subprocess.PIPE,                                     stderr=subprocess.PIPE) 
like image 190
Niklas B. Avatar answered Sep 22 '22 20:09

Niklas B.