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

Mohan Mohta mohan.mohta at gmail.com
Thu Jan 3 14:44:07 EST 2019


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

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





More information about the Python-list mailing list