In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you python read and write file with open how to achieve, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Case 1 (read)
First create a txt file that we want to read and write
The txt content is as follows:
Z Divine gift review document reading and writing
Blog Park address: https://www.cnblogs.com/ztcbug/
1. The basic realization of reading a file f = open ('test001.txt','r',encoding='utf-8') # open means to open. In (), the file path to be opened is read-only, and after opening, a value is assigned to f. If the read file is in Chinese, encoding= utf-8 is the encoding format print (f.read ()) # read means to read, and fjill read () means to read all the data in f. Then print output f.close () # after reading, and then open f to close, close () means to close, be sure to close
The returned result is as follows:
Z Divine gift review document reading and writing
Blog Park address: https://www.cnblogs.com/ztcbug/
The disadvantage is that if a program error occurs before we close the file, then the opened file is not closed.
2. Read the file intermediate implementation try: # first of all, try. If the program is opened and a series of operations are read after it is opened, the program f = open ('test001.txt','r',encoding='utf-8') file = f.read () print (file) finally: # regardless of whether the program in try reports an error or not Then execute the following closing if f: # to determine whether f is open, if it is not open, it does not need to be closed, if it is open, close f.close ()
The returned result is as follows:
Z Divine gift review document reading and writing
Blog Park address: https://www.cnblogs.com/ztcbug/
You can see that the basic implementation has been optimized this time, and we have closed the opened file regardless of whether the error is reported or not.
3. The ultimate realization of reading files
The above kind is good, but the code is not so concise. We can write it in with open way.
With open ('test001.txt','r',encoding='utf-8') as f: file = f.read () print (file)
The returned result is as follows:
Z Divine gift review document reading and writing
Blog Park address: https://www.cnblogs.com/ztcbug/
Case 2 (writing) 1. The basic implementation of writing to a file
Let's still use the file we just had. The content in the file just now is:
Z Divine gift review document reading and writing
Blog Park address: https://www.cnblogs.com/ztcbug/
At this point, we re-write data to the file:
F = open ('test001.txt','w',encoding='utf-8') # open opens the file to be written.' w' means to write. If there is a Chinese encoding to encode f.write ('the weather in Beijing is very beautiful today!') # write means to write, write the contents in () to the f file f.close () # and close it after writing
Then at this point, let's take a look at the contents of the test001.txt file.
Write successful!
2. Write to the file to achieve with open ('test001.txt','w',encoding='utf-8') as f: # using the with method f.write (' go for a braised chicken rice today!!') # write directly
Then at this point, let's take a look at the contents of the test001.txt file.
The above is all the contents of the article "how to read and write python File with open". 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.