tutorial introduction file input and output the following program opens file and reads its contents line by linef open("foo txt"line readline(while lineprint lineprint(line,end=''line readline( close(returns file object invokes readline(method on file trailing ',omits newline character use in python the open(function returns new file object by invoking methods on this objectyou can perform various file operations the readline(method reads single line of inputincluding the terminating newline the empty string is returned at the end of the file in the examplethe program is simply looping over all the lines in the file foo txt whenever program loops over collection of data like this (for instance input linesnumbersstringsetc )it is commonly known as iteration because iteration is such common operationpython provides dedicated statementforthat is used to iterate over items for instancethe same program can be written much more succinctly as followsfor line in open("foo txt")print lineto make the output of program go to fileyou can supply file to the print statement using >>as shown in the following examplef open("out"," "open file for writing while year <numyearsprincipal principal ( rateprint >> ,"% % (year,principalyear + close(the >syntax only works in python if you are using python change the print statement to the followingprint("% % (year,principal),file=fin additionfile objects support write(method that can be used to write raw data for examplethe print statement in the previous example could have been written this wayf write("% % \ (year,principal)although these examples have worked with filesthe same techniques apply to the standard output and input streams of the interpreter for exampleif you wanted to read user input interactivelyyou can read from the file sys stdin if you want to write data to the screenyou can write to sys stdoutwhich is the same file used to output data produced by the print statement for exampleimport sys sys stdout write("enter your name :"name sys stdin readline(in python this code can also be shortened to the followingname raw_input("enter your name :" lib fl ff