Files (Write and Read) in Python
In this post, you will learn how to work with files (write and read) so that you can use that same knowledge and thus evolve.
open(file, mode) |
Open the file |
write(text) |
Write on file |
read() |
Read the file content |
close(file) |
Close the file |
'w' |
Write mode |
'r' |
Read mode |
- it is necessary to always close the file.
writeFile = open('File-Write-and-Read.txt', 'w')
writeFile.write('I\'m writing on the first line of this file.\n')
writeFile.write('Now I\'m writing on the second line.')
writefile.close()
readFile = open('File-Write-and-Read.txt', 'r')
print(readFile.read())
'''
I'm writing on the first line of this file.
Now I'm writing on the second line.
'''
readFile.close()
Don’t forget to watch the video and you can always read this post in Portuguese.