In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the methods of python file operation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
How to close a file
When the file operation is over, we'd better take the initiative to close the file. Although Python has a garbage collection (garbage collector) mechanism to clean up unused objects, it's best to close the files yourself.
The easiest way is to:
F = open ("app.log",'r') do_something () f.close ()
However, this method is not safe, because an exception may occur during other operations, the program exits, and the statement to close the file will not be executed.
Therefore, it can be dealt with in terms of sentences:
Try: F = open ('app.log', 'r') do_something () finally: f.close ()
The instruction to close the file is executed regardless of whether an exception occurs.
But the official best use of python is:
With open ('app.log', 'r') as f:
Do_something ()
With this usage, we do not have to call the close () method, which is executed inside the with statement program, regardless of whether there is an internal exception or not. The with statement is called the context manager, we can ignore the principle of this for a while, as long as we know that using the with statement, the operation of closing the file will be executed automatically, which is also the best officially recommended usage, which is easier to write than phraseology.
File operation
Write to a file
Two main methods are introduced.
1.write () method
The argument to this method is a separate string, such as:
Lines = ['line1',' line2'] with open ('filename.txt', 'w') as f: s =' 'for data in lines: s + = data s + ='\ n' f.write (s)
In fact, it is better to use the join function:
Lines = ['line1',' line2'] with open ('filename.txt',' w') as f: f.write ('\ n'.join (lines))
2.writelines () method
A parameter is a set of iterating strings, such as
Lines = ['line1',' line2'] with open ('filename.txt', 'w') as f: new_lines = [] for data in lines: new_lines.append (data+'\ n') f.writelines (new_lines)
In fact, in a more elegant way of writing, you can use the generator:
Lines = ['line1',' line2'] with open ('filename.txt',' w') as f: f.writelines ("% s\ n"% l for lin lines)
Read a file
Here are four uses, all of which have been opened by default:
1.read ()
Result = f.read ()
What is returned here is the content of the file, which is the result of the str type. This method also takes a parameter of a numeric type that specifies how much to read. If omitted or negative, the entire contents of the file are returned.
2.readline ()
Result = f.readline ()
The string is also returned, but only one line of content. If you continue to call, the next line will be returned.
3.readlines ()
Result = f.readlines ()
What is returned here is a list, but when the data is large, this usage takes up a lot of memory and is not recommended when there is a large amount of data.
4. Loop the file object directly
For line in f: print line do_something ()
This usage saves memory, is fast, and the code is simple.
Result = f.readlines ()-result = list (f)
The results returned by these two methods are the same.
Obviously we recommend the fourth usage.
How to deal with large files
The main problem with large files is that they take up a lot of memory. We can't read all the contents of the file into memory at once. The best thing to do is as follows:
With open ("log.txt") as f: for line in f: do_something_with (line)
It is also faster to read one line at a time, using the with statement, regardless of whether there is an internal exception or not, the file object will be closed at the end, so this is highly recommended when dealing with large files.
This is the end of the content of "what are the methods for python to operate files?" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.