Can something wrong happen with the following implementation?
def ReadFromFile(file_name): return [line for line in open(file_name)] def AppendToFile(new_line, file_name): open(file_name, 'a').write(new_line) I am not explicitly calling close() method after reading / writing to the file. My understanding is that semantically the program has to behave as if the file is always closed at the end of each function.
Can the following use of these functions give unexpected results, e.g.
original_lines = ReadFromFile("file.txt") for line in original_lines: AppendToFile(line, "file.txt") modified_lines = ReadFromFile("file.txt") I would expect e.g. len(modified_lines) == len(original_lines) * 2. Can that ever not be the case?
with open(...)? This will make sure file is closed.