• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Files (Write and Read) in Python

October 20, 2021 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.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved