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--
This article mainly introduces "how python reads files". In daily operation, I believe many people have doubts about how python reads files. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to read files by python". Next, please follow the editor to study!
Open a file object
Use the open () function to open the file object. You must specify a file path. You can use either relative or absolute paths. You can choose to read and write incoming files, which is read-only by default. You can pass the file encoding method, which defaults to gbk.
F = open ('Zen.txt',' ritual, encoding= "utf-8") # Open a file object using read mode. The passed parameters are file relative position and read-write mode, and the encoding format is utf-8
Common identifiers are as follows:
R Open the file as read-only, read from the beginning of the file, and no file will report an error. W open the file in write-only mode, create a new file if there is no file, and write the file from scratch if there is one. An open the file as an append, create a new file if there is no file, and write the file from the end of the document if there is one. A + read-write mode, writing can only be written at the end of the file. W + is readable and writable, and the difference with a + is to empty the contents of the file. R + is readable and writable. The difference between r + and a + is that it can be written anywhere in the file.
Read the contents of the file
The contents of the file can be read using a series of functions of read (), as detailed in the code (requires the file to be readable in read-write mode, that is, the file cannot be read in write-only mode).
F = open ('Zen.txt',' rations, encoding= "utf-8") # Open the file object in read-only mode, and the file encoding format is utf-8f.read () # once the text is read, it is easy to overflow when the file is too large, you can specify the file size f.readline () # read one line of text data at a time, f.readlines () # read the text at one time and generate a list of strings by column
Write the contents of the file
The contents of a file can be written using a series of functions of write (), as detailed in the code (writable in file mode is required, that is, you cannot write to a file in read-only mode).
F = open ('Zen.txt', 'walled, encoding= "utf-8") # Open the file object in write-only mode. The file encoding format is utf-8str = "The Zen of Python, by Tim Peters" seq = {' Beautiful is better than ugly.','Explicit is better than implicit.','Simple is better than complex.'} f.write (str) # write (). The parameter is a string. The argument to .f.writelines (seq) # writelines () is a sequence, such as a list, which iterates to help you write to the file.
Close the file
The file must be closed after use, because the file object takes up system resources, and you can close the file using the close () method.
F = open ('Zen.txt', 'walled, encoding= "utf-8") # Open the file object in write-only mode. The file encoding format is utf-8str = "The Zen of Python, by Tim Peters" seq = {' Beautiful is better than ugly.','Explicit is better than implicit.','Simple is better than complex.'} f.write (str) # write (). The parameter is a string. The argument to .f.writelines (seq) # writelines () is a sequence, such as a list, which iterates to help you write to the file. F.close ()
Use try-catch to grab IOError
Because IOError can be generated when a file is read or written, once an error occurs, the subsequent f.close () will not be called. So, in order to ensure that the file is closed correctly regardless of whether there is an error or not, we can use try-catch-finally.
Try: F = open ("utf-8") print (f.read ()) finally:# when reading and writing generates IOError, use finally to close the file if f: f.close ()
Use the with statement
Using the with statement can help us call the close () method so that you don't have to write the try-catch-finally statement.
At this point, the study on "how python reads files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.