0
class stdout(object):
    def __init__(self,realout,prefix):
        self.file = realout
        self.at_start_of_line = True
        self.prefix=prefix
    def write(self, text):
        if len(text):
            if self.at_start_of_line:
                text = self.prefix + text

            self.file.write(text)

            self.at_start_of_line = (text[-1] == '\n')
sys.stdout=stdout(sys.stdout,"OUT  ["+f+"]> ")




  File "C:\Users\James\Desktop\run.py", line 13, in write
    self.file.write(text)
  File "C:\Users\James\Desktop\run.py", line 10, in write
    if self.at_start_of_line:
  AttributeError: 'stdout' object has no attribute 'prefix'

It's not the whole thing but the error is really confusing

4
Contributors
4
Replies
22
Views
3 Years
Discussion Span
Last Post by james.lu.75491856
0

This looks like you are using class, not class instance, so the instance variable is not initialized.

Edited by pyTony

0
from subprocess import *
import sys
class stdout(object):
    def __init__(self,realout,prefix):
        self.file = realout
        self.at_start_of_line = True
        self.prefix=prefix
    def write(self, text):
        if len(text):
            if self.at_start_of_line:
                text = self.prefix + text

            self.file.write(text)

            self.at_start_of_line = (text[-1] == '\n')
sys.stdout=stdout(sys.stdout,"OUT  ["+f+"]> ")
while True:
    f = raw_input("URL:")
    p = Popen("C:/python27/python.exe "+f,shell=True)#,stdin=PIPE,stdout=PIPE,stderr=PIPE)
    print"Press enter to read from pipes"
    print"Press Control-C to terminate"
    while not p.poll():
        try:
            d = raw_input("IN   ["+f+"]> ")
            if d == '':
                d = None
            try:
                output,errors = p.communicate(d)
            except:
                pass
            if errors.strip():
                print "ERROR["+f+"]>"
                print errors
        except KeyboardInterrupt:
            print"Terminating..."
            p.terminate()
            print"Terminated."
            break
This question has already been answered. 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.