title: file read/write Popen python
body: <p>I'm trying to Read/Write process file with command <code>grep</code> and <code>cut</code> to get part of the IP address using pipe operator, here is the process simply written.</p>
  <pre><code>import subprocess as sub
  import shlex
  
      def discover_Host():    
       with open(&quot;Get_IP.txt&quot;, &quot;w+&quot;) as q_process:
         cmd1 = &quot;grep Host xmap_ip.gnmap&quot;
         cmd2 = &quot;cut -d ' ' -f 2&quot;
         arg1 = shlex.split(cmd1)
         arg2 = shlex.split(cmd2)
      
         proc1 = sub.Popen(args1, stdout = sub.PIPE)
         proc2 = sub.Popen(args2, stdin = proc1.stdout, stdout = q_process)
      
         proc1.stdout.close()
         out, err = proc2.communicate()
      return
  </code></pre>
  <p>Now the file <code>Get_IP.txt</code> contains data like this <code>Host: 172:22:179:156 (abc.efg.net) Status: Up</code> so I'm trying to get only IP address from it as I run the <strong>grep</strong> and <strong>cut</strong> command with <strong>pipe</strong> directly on the terminal it works but as I try it with <strong>SubProcess</strong> it doesn't write on the file.</p>