hello sir,

I have doubt that how to redirect the ouput text of a command (while working on unix cmd prompt) to a file using python.please help me with sample code.

Recommended Answers

All 11 Replies

Here is an example of redirecting command 'python -V' to a file with the subprocess module

import subprocess

with open('foo.out', 'wb') as ofh:
    process  = subprocess.Popen(['python', '-V'], stdout = ofh, stderr = ofh)

with open('foo.out', 'rb') as ifh:

thank you very much sir

You may need to add the call process.communicate() to wait for command termination.

i have another doubt that i need to search for a word called "build" and need to print the word after that(build) ,the word buld is at middle of line

my code is

 for line in result:
   if "build"in line: jbulid= line.split()
    print jbuild

please corrct that code

I would use re module

import re
for line in result:
    match = re.search(r'\bbuild\W+(\w+)', line)
    if match is not None:
        word = match.group(1)

it prints the numerical value, but i need next word after"bulid" to print

can you post an example line ?

Java(TM) 2 Runtime Environment, Standard Edition (build pxa64dev-20111020 (SR13 ))

Which word do you want after build ?


Use match = re.search(r'\bbuild\s+([-\w]+)', line).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.