Reputation: 6891
I'm trying to get a few filenames by executing this command in os.popen :
ls /etc/tor/statistiekjes/ |egrep dns
But when I run my script I get :
<open file 'ls /etc/tor/statistiekjes/ |egrep dns', mode 'r' at 0xb7786860>
egrep: write error: Broken pipe
Code :
lscmd = "ls /etc/tor/statistiekjes/ |egrep "+FILE
print lscmd
inputList=os.popen(lscmd,'r')
File is an argument past to the script to grep on
Upvotes: 0
Views: 3376
Reputation: 879591
For this particular problem, you could use native Python calls:
import os
import re
for name in (name for name in os.listdir('/etc/tor/statistiekjes/')
if re.search(FILE,name)):
print(repr(name))
However, you are probably looking for a more general solution to calling external programs. In that case, use subprocess
instead of os.popen
, since os.popen
is deprecated:
import subprocess
import shlex
proc1 = subprocess.Popen(shlex.split('ls /etc/tor/statistiekjes/'),
stdout=subprocess.PIPE)
proc2 = subprocess.Popen(shlex.split('egrep {pat}'.format(pat=FILE)),
stdin=proc1.stdout,
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
out,err=proc2.communicate()
print(out)
See "Replacing shell pipeline".
PS. subprocess.Popen
has a shell=True
parameter which could also be used. However, it is best to avoid using shell=True
if possible. It is a security risk.
Upvotes: 3
Reputation: 13251
You can use subprocess.Popen, with shell=True flags:
from subprocess import Popen, PIPE
lscmd = "ls /etc/tor/statistiekjes/ |egrep "+FILE
inputList = Popen(lscmd, shell=True, stdout=PIPE).communicate()[0]
print inputList
Enjoy.
Upvotes: 2