Computer Science, asked by robiterang2922, 1 year ago

How to safely open/close files in Python?

Answers

Answered by Anonymous
1

file = open("name.txt","r")

content = file.read()

file.close()

Note by doing this, the contents of the file will be erased so you gotta be smart. Just follow this.

file = open("name.txt","r+")

backup = file.read()

content = backup

file.write(content) #you can write extra contents.

file.close()

By this your content won't be erased.

Similar questions