In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use python with file object statements, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Statements with file objects
We often need to read data from the file and write the data to the file. The most common way is to simply open a file using the built-in open () function, which creates a file object that we can manipulate.
> # Create a text file that has the text: Hello World!... # Open the file and append some new data. Text_file0 = open ("hello_world.txt", "a"). Text_file0.write ("Hello Python!")... # Open the file again for something else... Text_file1 = open ("hello_world.txt"). Print (text_file1.read ())... Hello World!
In the previous code snippet, we started with a text file with the text "Hello World!". Then we append some new data to the file. However, after a while, we want to process the file again. When we read the text file, it still has old data. In other words, the additional text is not included in the text file.
This is because we did not close the file object at first. If you do not close the file, you cannot save the changes. Indeed, we can close () explicitly call this method on the file object. However, we can do this using the "with" statement, which automatically closes the file object for us, as shown below. After we have finished working on the file, we can verify that the file is closed by accessing the closed property of the file object.
With open ("hello_world.txt", "a") as file:... File.write ("Hello Python!"). With open ("hello_world.txt") as file:... Print (file.read ()). Print ("Is file close?", file.closed). Hello World!Hello Python!Hello Python! Is file close? True
In more general terms, the with statement is the syntax for using the context manager in Python. The previous example involves file manipulation because these files are shared resources and we are responsible for releasing them. The context manager can help us get the job done. As shown earlier, when the file operation is finished, the file is automatically closed using the with statement.
The above is all the content of the article "how python uses statements with file objects". 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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.