+ 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()
1 Respuesta
+ 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