Hi,
I am using Python 2.4 on Windows.

I'm trying to save the contents of the debug window. I managed to redirect all print statements so that it prints on the debug window as well as saves a copy into a text file. The code is shpwn below:

import sys
import string

open('C:\Users\djeganat\Desktop\\todo.txt', 'w').close() 

class RedirectOutput:

    def __init__(self, stdout):
        self.stdout = stdout

    def write(self, s):
        self.stdout.write(s)
        f = open('C:\Users\djeganat\Desktop\\todo.txt', 'a')
        f.write(s)
        f.close()

# redirect standard output
sys.stdout = RedirectOutput(sys.stdout)


print "abcd",
print "\n xyz"
var = raw_input("Enter something: ")

This works perfectly fine but does not save the user input. I understand that in order to do this I need to redirect stdin. But, I don't seem to be doing it right. Could you show me?

Thanks!

I think you could try and use my brand new module 'adaptstrings', see here http://www.daniweb.com/software-development/python/code/389929 . The following code works from me

from adaptstrings import adapt_as_opener
import sys

@adapt_as_opener
def redirected_input(ifh, filename):
    with open(filename, "w") as ofh:
        while True:
            line = ifh.readline()
            ofh.write(line)
            yield line
            
sys.stdin = redirected_input(sys.stdin, "foo.txt")

for i in range(10):
    s = raw_input("Enter something: ")
    print "received ", s

Some small changes may be necessary for python 2.4 however, can't you upgrade ?

Sadly, I can't upgrade :(. But, thanks a ton for your help :)

Is there any way I could do it just the way I've done it for stdout?

Is there any way I could do it just the way I've done it for stdout?

Apparently yes, you can, as long as you only use raw_input(). By tracing the previous code, I saw that it only calls readline(), so you can define a class with a readline() method

import sys

class Redir(object):
    def __init__(self, ifh, ofh):
        self.ifh, self.ofh = ifh, ofh
    def readline(self):
        line = self.ifh.readline()
        if line:
            self.ofh.write(line)
        return line
    

sys.stdin = Redir(sys.stdin, open("foo.txt", "w"))

for i in range(10):
    s = raw_input("Enter something: ")
    print "received: ", s

Ok, this is what I did.

import sys
import string

open('C:\Users\djeganat\Desktop\\todo.txt', 'w').close() 

class RedirectOutput:

    def __init__(self, stdout):
        self.stdout = stdout

    def write(self, s):
        self.stdout.write(s)
        f = open('C:\Users\djeganat\Desktop\\todo.txt', 'a')
        f.write(s)
        f.close()

class RedirectInput:

    def __init__(self, stdin):
        self.stdin = stdin

    def readline(self):
        ss = self.stdin.readline()
        ss = str(ss)
        f = open('C:\Users\djeganat\Desktop\\todo.txt', 'a')
        f.write(ss)
        f.close()


# redirect standard output
sys.stdout = RedirectOutput(sys.stdout)
sys.stdin = RedirectInput(sys.stdin)


print "abcd",
print "\n xyz"
var = raw_input("Enter something: ")

But, I get an error

Traceback (most recent call last):
File "C:\Users\djeganat\Desktop\example.py", line 37, in ?
var = raw_input("Enter something: ")
TypeError: object.readline() returned non-string

Can you tell me what I did wrong?

Thanks!

Add return ss at the end of readline(). Also you don't need ss = str(ss). And don't write ss if it is then empty string.

Be a part of the DaniWeb community

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