0

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.

Edited by pyTony: Cleaned out code formatting

3
Contributors
11
Replies
116
Views
3 Years
Discussion Span
Last Post by Gribouillis
0

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:
    print(ifh.read())
0

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

Edited by Gribouillis

0

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

Edited by manohar1111: forget

0

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)
        print(word)

Edited by Gribouillis

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.