0

I am using subprocess module, which Popen class output some results like:

063.245.209.093.00080-128.192.076.180.01039:HTTP/1.1 302 Found 063.245.209.093.00080-128.192.076.180.01040:HTTP/1.1 302 Found

and here is the script I wrote:

import subprocess, shlex, fileinput,filecmp
proc = subprocess.Popen('egrep \'^HTTP/\' *', shell=True,      stdin=subprocess.PIPE, stdout=subprocess.PIPE,)
stdout_value = proc.communicate()[0]
print 'results:'
print stdout_value

My question is: how to convert/record the results from stdout into a file?

I appreciate all your responses and helps!

2
  • Are you asking how to write stdout_value to a file? Or how to pass a file into stdin=? I would suggest you do the former Commented Dec 1, 2010 at 1:07
  • Open a file and print into it? I'm not sure that's what you want, but you may try: f=open('log.txt','w'); print>>f, stdout_value Commented Dec 1, 2010 at 1:17

2 Answers 2

1
import subprocess
import glob

def egrep(pattern, *files):
    """ runs egrep on the files and returns the entire result as a string """
    cmd = ['egrep', pattern]
    for filespec in files:
        cmd.extend(glob.glob(filespec))
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    return proc.communicate()[0]

results = egrep(r'^HTTP/', '*')
print 'results:'
print results

# write to file
with open('result_file.txt', 'w') as f:
    f.write(results)
Sign up to request clarification or add additional context in comments.

Comments

0

One or any of the stdin, stdout, and stderr arguments to subprocess.Popen() can be file objects (or a file descriptor), which will cause the program to read from or write to the given files.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.