Python Forum

Full Version: How to read a file using stdin?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So far I have worked with this script which assigns the first line of the file to variable a and the second line to variable b. But I want to read all the lines in the file and print it on screen.

import sys

class Redirection(object):
        def __init__(self, in_obj, out_obj):
                self.input = in_obj
                self.output = out_obj

        def readline(self):
                res = self.input.readline()
                self.output.write(res)
                return res

if __name__ == '__main__':
        if not sys.stdin.isatty():
                sys.stdin = Redirection(in_obj = sys.stdin, out_obj = sys.stdout)

        a = input('Enter a string: ')
        b = input('Enter another string: ')
        print('Entered strings are: ', repr(a), ' and ', repr(b))
Please do not repeat threads. It is against forum rules.
It't OK to bump an old thread to bring it to attention