For beginners , A detailed and clear guide is important . today , Cat cat is with us , study hard Python File read / write contents , This part of the content is particularly commonly used , It will be of great benefit to both work and practice . Learning is a gradual process , More haste, less speed. . The article is longer , I suggest you collect , For review and reference .
1、 How to write list data to a file ?
2、 How to read from a file ?
3、 Reading and writing tasks with diverse needs
4、 from with Statement to context manager
How to write list data to a file ?
First , Let's take a look at the following code , And think : Is there a problem with this code , If there's a problem , How to change ?
li = ['python',' is',' a',' cat']
with open('test.txt','w') as f:
f.write(li)
Copy code
Now publish the answer , This code will report an error :
TypeError Traceback (most recent call last)
<ipython-input-6-57e0c2f5a453> in <module>()
1 with open('test.txt','w') as f:
----> 2 f.write(li)
TypeError: write() argument must be str, not list
Copy code
The idea of the above code is to list Write the contents of the list txt In file , But the report is wrong TypeError: write() argument must be str. That is to say ,write() Method must accept a string (str) Parameters of type .
Python Built in str() Method , You can return a string version of an object (Return a string version of object). therefore , In the example above , Let's try f.write(li) Change it to f.write(str(li)) , First, do the conversion of string type . The code is slightly .
There's no wrong report this time , But it's silly to open a file , What is written is “['python',' is',' a',' cat']”. How can I write “python is a cat” Well ?
There is another file write operation writelines() Method , The parameters it receives are sequences of strings (sequence), The effect of actual writing is to splice all strings together . The string itself is a sequence , So when the parameter is a string ,writelines() Method is equivalent to write().
# following 3 Two writing methods are equivalent , Are written to the string “python is a cat”
In [20]: with open('test.txt','w') as f:
...: f.writelines(['python',' is',' a',' cat'])
...: f.writelines('python is a cat')
...: f.write('python is a cat')
# following 2 Two writing methods are equivalent , Are string versions written to the list “['python',' is',' a',' cat']”
In [21]: with open('test.txt','w') as f:
...: f.write(str(['python',' is',' a',' cat']))
...: f.writelines(str(['python',' is',' a',' cat']))
# As a counterexample , The following expressions are wrong :
In [22]: with open('test.txt','w') as f:
...: f.writelines([2018,'is','a','cat']) # Contains non string
...: f.write(['python','is','a','cat']) # The string
Copy code
It can be seen from the above that , When a multi segmented string exists in the list , Use writelines() Method , If the string is an entire segment , That's direct use write() Method . If you want to write a file as an entire list , Just use str() Methods to do the next transformation .
The problem is not over yet , If there is only one element in the list that is not a string , And take out all the elements , What shall I do? ?
Then you can't use it directly write() and writelines() 了 , We need to use it first. for loop , Take out each element , one by one str() Handle .
In [37]: content=[1,' is',' everything']
In [38]: with open('test.txt','w') as f:
...: for i in content:
...: f.write(str(i))
Copy code
It should be noted that ,writelines() No line wrapping . If you want to wrap lines between list elements , One way is to add a newline character after each element “\n”, If you don't want to change the elements , It's best to use it. for loop , Add at the end when writing :for i in content: f.writelines(str(i)+“\n”).
Let's extend it , After the experiment , Numbers and Yuanzu types can also be used as write() Parameters of , No conversion required . however dict Dictionary type cannot , We need to use it first. str() With the . The dictionary type is quite special , It's best to use it. json.dump() Method to write to a file
To sum up ,write() Receive string parameters , It is applicable to write all contents to the file at one time ;writelines() The receiving parameter is a sequence of strings , It is applicable to writing the contents of the list line by line to the file .str() return Python The string version of the object , Pay attention to use .
How to read from a file ?
There are the following ways to read from a file :
file.read([size])
Read the specified number of bytes from the file , Read all if not given or negative .
file.readline([size])
Read entire line , Include "\n" character .
file.readlines([sizeint])
Read all the lines and return the list , If given sizeint>0, Set how many bytes to read at a time , This is to reduce the reading pressure .
In short , Without parameters ,read() Corresponding write(), Read all ;readlines() Corresponding writelines(), Read all ( With line breaks ) And return... As a list , The contents of each newline are treated as an element of the list .
In [47]: with open('test.txt','r') as f:
...: print(f.read())
1 is everything.
python is a cat.
this is the end.
In [48]: with open('test.txt','r') as f:
...: print(f.readlines())
['1 is everything.\n', 'python is a cat.\n', 'this is the end.']
Copy code
however , The above two methods have one drawback , When the file is too large , Read too much at once , It will put great pressure on the memory . There is another read operation readline() Method , It can be read line by line .
In [49]: with open('test.txt','r') as f:
...: print(f.readline())
1 is everything.
Copy code
readline() Read the first line and return , Call again f.readline(), Will read the next line .
So it seems ,readline() It's too clumsy . that , Is there any way to read the contents of a file gracefully ?
Looking back readlines() Method , It returns a list . Isn't that strange , Why should good content be returned as a list ?
Think about it again writelines() Method , Writing a list of strings to a file is what this guy does ,readlines() Method is exactly its reverse operation ! and writelines() The method should cooperate with for loop , So we put readlines() And for Cycle combination , See what happens .
In [61]: with open('test.txt','r') as f:
...: for line in f.readlines():
...: print(line)
1 is everything.
python is a cat.
this is the end.
# The read content contains a newline character , So we need to strip() Remove the newline
In [62]: with open('test.txt','r') as f:
...: for line in f.readlines():
...: print(line.strip())
1 is everything.
python is a cat.
this is the end.
Copy code
To sum up ,readline() Compare chicken ribs , It doesn't work very well ;read() Suitable for reading less content , Or when you need to process all the content at once ; and readlines() Use more , More flexible , because for A loop is an iterator , Load some content each time , Reduce memory pressure , It is also convenient to process data line by line .
Reading and writing tasks with diverse needs
The first two parts discuss several core methods of file reading and writing , The premise that they can work is , You need to open a file object first , Because only on the basis of file operators can you read or write .
Open the file with open() Method , So let's continue with this method .open() Method is used to open a file , And return the file object , This function is used for processing files , If the file cannot be opened , Will throw out OSError.
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open() Method parameters file( file ) It's necessary , The other parameters most commonly used are mode( Pattern ) and encoding( code ).
Let's talk about it first. encoding, Generally speaking , The encoding method of the open file shall be subject to the default encoding of the operating system , Chinese may be garbled , Need to add encoding='utf-8'.
In [63]: with open('test.txt','r') as f:
...: for line in f.readlines():
...: print(line.strip())
-----------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-63-731a4f9cf707> in <module>()
1 with open('test.txt','r') as f:
----> 2 for line in f.readlines():
3 print(line.strip())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 26: illegal multibyte sequence
In [65]: with open('test.txt','r',encoding='utf-8') as f:
...: for line in f.readlines():
...: print(line.strip())
wx Male z Number python Programming learning circle
python is a cat.
Copy code
Besides, mode, It specifies the mode in which the file is opened .
'r': Open in read-only mode ( default mode , It must be ensured that the file exists )
'w': Open in write only mode . If the file exists , Then empty the file , Then recreate ; If it does not exist , The new
'a': Open in append mode . If the file exists , Will be appended to the end of the file ; If the file does not exist , The new
quote
quote
common mode Combine
'r' or 'rt': The default mode , Text reading mode
'w' or 'wt': Open in text write mode ( The file is emptied before opening )
'rb': Open in binary read mode
'ab': Open in binary append mode
'wb': Open in binary write mode ( The file is emptied before opening )
'r+': Open in text read / write mode , The default write pointer starts at the beginning of the file , So it will override the file
'w+': Open in text read / write mode ( The file is emptied before opening )
'a+': Open in text read / write mode ( Can only be written at the end of the file )
'rb+': Open in binary read-write mode
'wb+': Open in binary read-write mode ( Empty before opening )
'ab+': Open in binary read-write mode
First look , There are many modes , however , They are just a combination of each other . It is recommended to remember the most basic w、r、a, Encounter special scenarios , Just look again .
from with Statement to context manager
The basic part is finished , The following is the advanced part . Know what it is , More to know why .
1、with Sentence is a must for beginners
First , I want to explain why I used it directly with sentence .with Statement is an elegant way to write and read files , This has been defaulted to Python Beginners must have some common sense . If you don't , Let's see if we can use it or not with Comparison of sentences :
# no need with The correct way to write a sentence
try:
f = open('test.txt','w')
f.writelines(['python',' is',' a',' cat'])
finally:
if f:
f.close()
# Use with The correct way to write a sentence
with open('test.txt','w') as f:
f.writelines(['python',' is',' a',' cat'])
Copy code
Because file objects take up operating system resources , And the number of files that the operating system can open at the same time is limited , therefore open() Method must be called after close() Method . in addition , Read / write operations may occur IO Abnormal condition , So add try…finally, I promise that in any case , Will call close() Method .
It is safe to write like this , But it's really tedious , If you are not careful, you may miss or make mistakes . and with Statement guarantees that close(), Just one line of code , Don't be too elegant ! therefore ,with The sentence is Python Beginners must know the skills .
2、 What is context manager ?
below , Here we go , What is context manager (context manager)?
The context manager is such an object : It defines the context that the program needs to establish when it runs , Handler entry and exit , The context management protocol is implemented , That is to say, in the object __enter__() and __exit__() Method .
__enter__(): Enter the runtime context , Returns the runtime context sensitive object ,with Statement will bind the return value to the target object .
__exit__(exception_type, exception_value, traceback): Exit runtime context , The definition is executed in the block ( Or terminate ) What should the context manager do after that . It can handle exceptions 、 Clean up the site or dispose of with Actions to be processed after the execution of statements in the block .
Be careful enter and exit There are two underscores before and after ,Python There are many similar methods in , They are very mysterious and powerful , Jianghu people often call it “ Black magic ”. for example , The iterator protocol implements __iter__ Method .
stay Python In the built-in type of , Many types support context management protocols , for example file,thread.LockType,threading.Lock wait .
The context manager cannot be used independently , They have to be related to with Combination ,with Statement can enter a runtime context before the code block runs ( perform _enter_ Method ), And exits the context at the end of the code block ( perform __exit__ Method ).
with Statement is suitable for accessing resources , Make sure that the necessary... Is executed regardless of whether an exception occurs during use “ clear ” operation , Release resources , For example, the file is automatically closed after use 、 Automatic acquisition and release of locks in threads .
3、 Custom context manager
except Python Built in type , Anyone can define their own context manager . Here's an example :
class OpenFile(object):
def __init__(self,filename,mode):
self.filename=filename
self.mode=mode
def __enter__(self):
self.f=open(self.filename,self.mode)
self.f.write("enter now\n")
return self.f # As as The value of the variable specified by the specifier
def __exit__(self,type,value,tb):
self.f.write("exit now")
self.f.close()
return False # The exception is passed out of the context
with OpenFile('test.txt','w') as f:
f.write('Hello World!\n')
Copy code
The final result of writing to the file is :
enter now
Hello World!
exit now
The context manager must also provide __enter__() and _exit_() Method definition , The lack of any one will lead to AttributeError.
Exceptions may occur during the execution of the context manager ,_exit_() The return value of will determine how the exception is handled : The return value is equal to False, Then the exception will be thrown back to the upper level ; The return value is equal to True, So this exception is ignored , Continue with the rest of the code .__exit()__ There are three parameters (exception_type, exception_value, traceback), That is, information about the exception .
4、contextlib Implement the context manager
In the above example , Writing a custom context manager is quite tedious , And can only be used at the class level . To better assist with context management ,Python Built in provides contextlib modular , Then it is very convenient to implement the function level context manager .
The module is essentially through the decorator (decorators) And generators (generators) To implement the context manager , Can act directly on a function / object , Instead of caring about enter__() and __exit() The concrete implementation of the method .
First transform the above example , Then we will explain it in comparison :
from contextlib import contextmanager
@contextmanager
def open_file(name):
ff = open(name, 'w')
ff.write("enter now\n")
try:
yield ff
except RuntimeError:
pass
ff.write("exit now")
ff.close()
with open_file('test.txt') as f:
f.write('Hello World!\n')
Copy code
contextmanager Is the decorator to be used ,yield Keyword turns a normal function into a generator .yield The return value of (ff) Equal to the above example __enter__() The return value of , That is to say as The value of the statement (f), and yield Before and after , Namely _enter_() and _exit_() The content of the method .
Use contextlib, Class definitions can be avoided 、_enter_() and __exit()__ Method , But we need to catch possible exceptions ( for example ,yield Only one value can be returned , Otherwise, it will lead to abnormality RuntimeError), therefore try…except Statement cannot ignore .
The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing