Jun-02-2018, 07:39 PM
Hi all! So my exercise is to read what's written in a text file and print it three times in three different ways. My solution is:
Thank you!
filename = 'learning_python.txt' with open(filename) as file_object: contents = file_object.read() print(contents.strip()) with open(filename) as file_object: lines = file_object.readlines() for line in lines: print(line.strip()) with open(filename) as file_object: for line in file_object: print(line.rstrip())But I don't understand why this isn't working (it prints contents of the file only once instead of thrice):
filename = 'learning_python.txt' with open(filename) as file_object: contents = file_object.read() print(contents.strip()) for line in file_object: print(line.rstrip()) lines = file_object.readlines() for line in lines: print(line.strip())I've read through the material again and it says nothing about this and I think it's important to understand this.
Thank you!