Agreeing with the other poster that it's probably not the best way to handle it.

But for the sake of helping with subprocess:
https://docs.python.org/3.7/library/subprocess.html#popen-objects

Popen Objects don't have read() as the error says. That's on their .stdout and 
.stderr streams. So you'd want g_info.stdout.read() Or .stderr maybe, depending 
on what you're running and how it does its output. If you want them both to go 
to the same thing you can use stderr = subprocess.STDOUT instead of 
subprocess.PIPE, then both will end up in your .stdout stream.

And while it is indeed gonna be a quick thing that you're running, you have 
nothing in there that makes sure your subprocess actually runs and finishes 
before you're trying to read the results, which will bite you on anything more 
complicated.



-----Original Message-----
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom....@python.org] On Behalf Of 
Mohan Mohta
Sent: Thursday, January 03, 2019 2:44 PM
To: python-list@python.org
Subject: subprocess : AttributeError: 'Popen' object has no attribute 'read'

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

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


-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to