reading and writing files notice that within the for loop we have indicated to the print function that we want the end character to be rather than newlinethis is because the line string already possesses the newline character read from the file file contents iteration as suggested by the previous exampleit is very common to want to process the contents of file one line at time in fact python makes this extremely easy by making the file object support iteration file iteration accesses each line in the file and makes that line available to the for loop we can therefore writefile open('myfile txt'' 'for line in fileprint(lineend=''file close(it is also possible to use the list comprehension to provide very concise way to load and process lines in file into list it is similar to the effect of readlines(but we are now able to pre-process the data before creating the listfile open('myfile txt'' 'lines [line upper(for line in filefile close(print(lines writing data to files writing string to file is supported by the write(method of coursethe file object we create must have an access mode that allows writing (such as ' 'note that the write method does not add newline character (represented as '\ 'to the end of the string--you must do this manually an example short program to write text file is given belowprint('writing file' open('my-new-file txt'' ' write('hello from python!!\ ' write('working with files is easy \ ' write('it is cool \ ' close(