Supplement: there is no introduction to the usage of with open in this book.
If you have a dream, you need to realize his “Python file read and write, and then use the with open statement”.
Reading and writing files is the most common IO operation. Python has built-in functions for reading and writing files, and its usage is compatible with C.
Before we read and write files, we must understand that the function of reading and writing files on disk is provided by the operating system. Modern operating systems do not allow ordinary programs to operate disk directly. So, reading and writing files is to ask the operating system to open a file object (usually called a file descriptor), and then, by operatingRead data (read file) from this file object or write data to this file object (write file) as an interface provided by the system.
read file
To open a file object in the mode of reading files, use Python built-in.open()
Function, incoming file name and identifier:
>>> f = open('/Users/michael/test.txt', 'r')
The identifier’r’indicates reading, so we successfully open a file.
If the file does not exist,open()
The function throws one.IOError
Error, and give error codes and detailed information to tell you that files do not exist:
>>> f=open('/Users/michael/notfound.txt', 'r') Traceback (most recent call last): File "<stdin>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: '/Users/michael/notfound.txt'
If the file is opened successfully, next,callread()
The method can read all the contents of the file at once. Python reads the contents into memory and uses one.str
Object representation:
>>> f.read() 'Hello, world!'
The last step is to call.close()
Method to close file。Files must be closed after use because file objects occupy the resources of the operating system and the number of files that the operating system can open at the same time is limited:
>>> f.close()
Because files are likely to occur when they read and write.IOError
,Once a mistake is made, the latterf.close()
It will not be invoked. So, in order to ensure that the files can be shut down correctly, whether we make mistakes or not, we can use them.try ... finally
To realize:
try: f = open('/path/to/file', 'r') print(f.read()) finally: if f: f.close()
But it’s too complicated to be realistic every time.PythonIntroducedwith
Statement to automatically call us.close()
Method:
with open('/path/to/file', 'r') as f: print(f.read())
This is in front of us.try ... finally
It’s the same, but the code is better and simpler, and it doesn’t have to be called.f.close()
Method.
callread()
It reads all the contents of the file at once. If the file has 10G, the memory explodes. So, to be on the safe side, you can call it again and again.read(size)
Method reads up to size bytes at most each time. In addition, callingreadline()
You can read one line at a time.readlines()
Read all contents at once and return by rows.list
。Therefore, we need to decide how to call according to needs.
If the file is small,read()
Once read is most convenient; if the size of the file can not be determined, it can be called repeatedly.read(size)
Compare insurance; if it is a configuration file, call it.readlines()
The most convenient:
for line in f.readlines(): print(line.strip()) # Delete the last'\n'.
Write a document
Writing files is the same as reading files.The only difference is the call.open()
Function, incoming identifier'w'
perhaps'wb'
Represents writing text files or writing binary files.:
>>> f = open('/Users/michael/test.txt', 'w') >>> f.write('Hello, world!') >>> f.close()
You can call back and forth.write()
To write to a file, but make sure to call it.f.close()
To close the file. When we write a file, the operating system often does not write the data to disk immediately, but put it in the memory cache and write it slowly when it is free. Only callclose()
When the method is used, the operating system ensures that all data that is not written is written to disk. Forget to callclose()
The consequence is that the data may be written only to a part of the disk, and the rest is missing. So, still use.with
Statement is insurance:
with open('/Users/michael/test.txt', 'w') as f: f.write('Hello, world!')
To write specific encoded text files, please giveopen()
Function afferentencoding
Parameter, automatically converts the string to the specified encoding.
Character coding
To read non UTF-8 encoded text files, you need toopen()
Function afferentencoding
Parameters, for example, read GBK encoded files:
>>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk') >>> f.read() 'Test '
You may encounter some coding documents that are not standardized.UnicodeDecodeError
,Because there may be some illegal coded characters in the text file. In such a situation,open()
The function also receives one.errors
Parameter indicates how to deal with coding error. The simplest way is to directly ignore:
>>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk', errors='ignore')
Binary file
The default is to read text files, and UTF-8 encoded text files. To read binary files, such as pictures, videos, etc.,'rb'
The mode opens the file:
>>> f = open('/Users/michael/test.jpg', 'rb') >>> f.read() b'\xff\xd8\xff\xe1\x00\x18Exif\x00\x00...' # Byte represented by sixteen binary system
Summary: read and write files using with open statements in the future, instead of using F = open () as before
There are two ways to read and write multiple files:
with open('/home/xbwang/Desktop/output_measures.txt','r') as f: with open('/home/xbwang/Desktop/output_measures2.txt','r') as f1: with open('/home/xbwang/Desktop/output_output_bk.txt','r') as f2:
........
........
........
with open('/home/xbwang/Desktop/output_measures.txt','r') as f: ........ with open('/home/xbwang/Desktop/output_measures2.txt','r') as f1: ........ with open('/home/xbwang/Desktop/output_output_bk.txt','r') as f2: ........