Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

subprocess : AttributeError: 'Popen' object has no attribute 'read'

103 views
Skip to first unread message

Mohan Mohta

unread,
Jan 3, 2019, 2:44:20 PM1/3/19
to
Hello,
I am trying to grep the keyword (which I got from report_file ) from report_file

I tried multiple ways but am unable to get it to work.

Below are the methods I tried.

<Original Code>
fp=open(txt_file,'r')
for line in fp :
line=line.strip()
var1=line.lower()
g_info=subprocess.Popen('cat report_file| grep -i '+var1, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
g_info=g_info.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Error:
AttributeError: 'Popen' object has no attribute 'read'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<Modified Code -1 >
fp=open(txt_file,'r')
for line in fp :
line=line.strip()
var1=line.lower()
cmd='cat report_file| grep -i '+var1
g_info=subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
g_info=g_info.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Error:
AttributeError: 'Popen' object has no attribute 'read'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<Modified Code -2 >
fp=open(txt_file,'r')
for line in fp :
line=line.strip()
var1=line.lower()
cmd='cat report_file| grep -i '+var1
g_info=os.command(cmd)
g_info=g_info.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Result :
The Code executes but the output is in screen and does not get stored in a variable.
I am interested if I can achieve the same result with subprocess calls
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<Modified Code -3 >
fp=open(txt_file,'r')
for line in fp :
line=line.strip()
var1=line.lower()
cmd='cat report_file| grep -i '+var1
g_info=subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True).
g_info=g_info.stdout.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Error
AttributeError: 'Popen' object has no attribute 'read'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<Modified Code -3 >
fp=open(txt_file,'r')
for line in fp :
line=line.strip()
var1=line.lower()
cmd=['cat','report_file','|','grep','-i',serv_name]
g_info=subprocess.Popen(cmd)
g_info.wait()
try :
g_info=g_info.stdout.readlines()
print g_info
except AttributeError :
pass
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Result :
Nothing gets printed out

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Chris Angelico

unread,
Jan 3, 2019, 2:49:31 PM1/3/19
to
On Fri, Jan 4, 2019 at 6:46 AM Mohan Mohta <mohan...@gmail.com> wrote:
>
> Hello,
> I am trying to grep the keyword (which I got from report_file ) from report_file
>
> I tried multiple ways but am unable to get it to work.

How about, instead, you simply open the file and iterate through it,
looking for the keyword? 'grep' is irrelevant, and definitely 'cat' is
a waste of effort. Python code doesn't have to be written as a less
terse form of bash.

ChrisA

Mohan Mohta

unread,
Jan 3, 2019, 3:33:02 PM1/3/19
to
I am no expert in python but I found grep is lot faster in than the methods of reading files from python.... point me to direction if you know of anything faster I would appreciate it.

Chris Angelico

unread,
Jan 3, 2019, 3:40:43 PM1/3/19
to
On Fri, Jan 4, 2019 at 7:37 AM Mohan Mohta <mohan...@gmail.com> wrote:
> I am no expert in python but I found grep is lot faster in than the methods of reading files from python.... point me to direction if you know of anything faster I would appreciate it.
>

Try doing things the simple and easy way in Python, then figure out if
it's too slow. Only THEN should you worry about "faster".

ChrisA

marco....@colosso.nl

unread,
Jan 4, 2019, 6:10:51 AM1/4/19
to
Yeah, and once you find out you need to go faster and subsequently start looking into ways you *can* go faster, you finally end up with code similar to what is implemented in the grep family of tools.

Chris Angelico

unread,
Jan 4, 2019, 7:53:17 AM1/4/19
to
Maybe... but most likely not using a subprocess. But it's still
premature to worry about performance when you don't have any evidence
that your code is too slow.

ChrisA

Peter Otten

unread,
Jan 4, 2019, 9:05:55 AM1/4/19
to
Mohan Mohta wrote:

> Hello,
> I am trying to grep the keyword (which I got from report_file ) from
> report_file
>
> I tried multiple ways but am unable to get it to work.
>
> Below are the methods I tried.
>
> <Original Code>
> fp=open(txt_file,'r')
> for line in fp :
> line=line.strip()
> var1=line.lower()
> g_info=subprocess.Popen('cat report_file| grep -i '+var1,
> stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
> g_info=g_info.read() g_info=g_info.strip()
> info=g_info.strip('||')
> print g_info
> print info
> fp.close()

> Error:
> AttributeError: 'Popen' object has no attribute 'read'

You have to specify the stream/file, e. g.

g_info.stdout.read()

but when want both stdout and stderr your reading attempts may produce a
deadlock as the fine manual warns.

So with basic error checks:

import subprocess

def grep(file, wanted):
p = subprocess.Popen(
["grep", "-i", wanted, file],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdout, stderr = p.communicate()
if p.returncode:
raise Exception(stderr)
return stdout


txt_file = "wanted.txt"
report_file = "report.txt"

with open(txt_file) as fp:
for line in fp :
wanted = line.strip().lower()
print "Looking for:", wanted
print "Found:"
print grep(report_file, wanted)

> info=g_info.strip('||')

This does not do what you think it does -- it will remove all "|" chars from
the beginning and the end of the string, just like g_info.strip("|") and
g_info.strip("||||||||||||||||") would, i. e. the str.strip() argument is
treated like a set of chars.

0 new messages