How to write and close python files
This article mainly shows you "how to write and close python files", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to write and close python files" this article.
1. Change mode to a to achieve continuous write append, but the append does not break the line, so you need to use the newline symbol before the added text.
2. File closing must be closed when it comes to data security.
Example
Stream = open (file='./666', mode='w', encoding='utf-8') print (stream) record ='18 April 17:00 FPX Competition RNG'stream.write (record) # File write stream.close () # File closure must be closed related to data security # if the open file does not exist Then a new file stream = open (file='./text1.txt', mode='w', encoding='utf-8') print (stream) # can be added continuously by changing mode to a, but the addition will not help us to change the line So when we need to wrap the text, we need to use the newline symbol stream = open (file='./text1.txt', mode='a', encoding='utf-8') print (stream) record ='\ nApril 18 17:00 FPX competition RNG'stream.write (record) # file to write stream.close () above is all the content of the article "how to write and close python files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!