+ 1

can anybody tell me what has changed

file = open("newfile.txt", "r") print("Reading initial contents") print(file.read()) print("Finished") file.close() file = open("newfile.txt", "w") file.write("Some new text") file.close() file = open("newfile.txt", "r") print("Reading new contents") print(file.read()) print("Finished") file.close()

27th Mar 2019, 4:41 AM
胡軒豪
胡軒豪 - avatar
1 Antwort
+ 2
What do you mean, what has changed? If you run the code on Sololearn, the file newfile.txt probably won't exist, so you can't open and read it. Here's how you can check if the file exists: try: file = open("newfile.txt", "r") print("Reading initial contents") print(file.read()) print("Finished") file.close() except FileNotFoundError: print('File doesn\'t exist') Let me know if this doesn't answer your question
27th Mar 2019, 6:01 AM
Anna
Anna - avatar