We have an input file in which strings are stored. Our task is to put into the output file only those lines that are not repeated with the previous ones. The teacher forbade the use of arrays, so we should only compare strings using filename.readLine (). Sample input file:
apple apple apple apple banana bargain brick brick sample sample simple text text text Example file output:
apple banana bargain brick sample simple text Here is my code, which for some reason simply reprints the original file:
fileFrom = open ('data.txt', 'r') fileTo = open ('result.txt', 'w') line = fileFrom.readline() while line != '': if line == fileFrom.readline(): line = fileFrom.readline() else: fileTo.write(line + '\n') line = fileFrom.readline() fileFrom.close() fileTo.close() If you remove line = fileFrom.readline() in a loop, then it generally reprints only the first line. Obviously, the if condition is not satisfied, but I cannot understand why.