Opening files on Python | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Opening files on Python

file = open("filename.txt", "r") cont = file.read() print(cont) file.close() Displays an error when run. 'filename.txt' does exist in the correct folder, I believe. Would appreciate some feedback.

15th Mar 2018, 1:37 PM
Msizi
2 Respostas
+ 7
If you uncomment the upper block (write file block) you will see it works, but executing the read file block only gives the same error, curious...does it mean every file created during code execution is erased after code is interpreted completely? '''f = open("test.txt", "w") print('Opening test.txt for writing...') f.write("This text is written in test.txt") f.close() print('Write file success...closing file\n')''' print('Opening test.txt fo reading...') f = open("test.txt", "r") cont = f.read() print("Content of test.txt file is:\n") print(cont) f.close() print('\nRead file success...closing file.') *Tested in Code Playground only
15th Mar 2018, 2:37 PM
Ipang
+ 1
If you run it on a PC, have you tried with the full qualified path of the file (C:/.../.../filename.txt) ?
15th Mar 2018, 2:07 PM
Geo
Geo - avatar