How to safely open/close files in Python?
Answers
Answered by
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
Science,
7 months ago
History,
7 months ago
Computer Science,
1 year ago
Physics,
1 year ago
Math,
1 year ago