Python file read and write, then use the with open statement to read and write python files, and then use the with open statement

Taking notes and turning your own dreams will come true https://www.cnblogs.com/ymjyqsx/p/6554817.html

Python file read and write, 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 reading and writing files, we must first understand that the functions of reading and writing files on the disk are provided by the operating system. Modern operating systems do not allow ordinary programs to directly operate the disk. Therefore, reading and writing files is to request the operating system to open a file. A file object (usually called a file descriptor), and then, through the interface provided by the operating system, data is read from the file object (read file), or data is written to the file object (write file).

read file

To open a file object in file-reading mode, use Python's built-in open()functions, passing in the file name and identifier:

>>> f = open('/Users/michael/test.txt', 'r')

The identifier 'r' means read, so we have successfully opened a file.

If the file does not exist, the open()function will throw an IOErrorerror with an error code and detailed information telling you that the file does 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, calling read()the method can read the entire contents of the file at once, and Python reads the contents into memory, represented by an strobject :

>>> f.read()
'Hello, world!'

The final step is to call the close()method to close the file. The file must be closed after use, because the file object will 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()

Since the file may be generated when reading and writing IOError, once an error occurs, the following f.close()will not be called. So, to ensure that the file is properly closed regardless of errors, we can use try ... finally:

copy code
try:
    f = open('/path/to/file', 'r') print(f.read()) finally: if f: f.close()
copy code

But it's too cumbersome to write like this every time, so Python introduces a withstatement to automatically call the close()method for us:

with open('/path/to/file', 'r') as f: print(f.read())

This try ... finallyis the same as before, but the code is more concise and does not have to call the f.close()method.

The call read()will read the entire content of the file at one time. If the file has 10G, the memory will explode. Therefore, to be on the safe side, you can call read(size)the method repeatedly, reading at most size bytes each time. Alternatively, the call readline()can read one line at a time, and the call readlines()reads everything once and returns by line list. Therefore, it is necessary to decide how to call according to needs.

If the file is small, it is read()most convenient to read it at one time; if the file size cannot be determined, it is safer to call it repeatedly read(size); if it is a configuration file, it is readlines()the most convenient to call:

for line in f.readlines():
     print(line.strip()) #delete the '\n' at the end

write file

Writing a file is the same as reading a file, the only difference is that open()when calling the function, an identifier is passed in 'w'either 'wb'to write a text file or a binary file:

>>> f = open('/Users/michael/test.txt', 'w') >>> f.write('Hello, world!') >>> f.close()

You can call it repeatedly write()to write to the file, but be sure to call f.close()it to close the file. When we write a file, the operating system often does not write the data to the disk immediately, but stores it in the memory cache, and writes it slowly when it is free. Only close()when the method is called does the operating system guarantee that all unwritten data will be written to disk. The consequence of forgetting to call close()is that only part of the data may be written to disk, and the rest is lost. Therefore, it is still withsafe to use a statement:

with open('/Users/michael/test.txt', 'w') as f: f.write('Hello, world!')

To write a text file with a specific encoding, open()pass encodingin parameters to the function to automatically convert the string to the specified encoding

Character Encoding

To read non-UTF-8 encoded text files, you need to open()pass encodingparameters to the function, for example, to read GBK encoded files:

>>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk') >>> f.read() '测试'

When you encounter some files with irregular encoding, you may encounter UnicodeDecodeErrorbecause some illegal encoding characters may be mixed in the text file. In this case, the open()function also receives a errorsparameter that indicates what to do if an encoding error is encountered. The easiest way is to just ignore:

>>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk', errors='ignore')

binary file

The default mentioned above is to read text files, and they are UTF-8 encoded text files. To read binary files, such as pictures, videos, etc., 'rb'open the file with mode:

>>> f = open( ' /Users/michael/test.jpg ', ' rb ' ) >>> f.read() b ' \xff\xd8\xff\xe1\x00\x18Exif\x00\x00.. . ' #byte in hex

 

 

Summary: In the future, use the with open statement to read and write files, and don't use f = open() as before.

 

   For reading and writing multiple files, it can be written in the following two ways:

copy code
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:
       ........
       ........
       ........
copy code
copy code
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: ........

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324781709&siteId=291194637