How to read and write files in a Python Program
This article describes how to read and write files in a Python program. It is a basic knowledge of Python learning. For more information, see
Reading and Writing files is the most common IO operation. Python has built-in functions for reading and writing files. Its usage is compatible with C.
Before Reading and Writing files, we must first understand that the functions of reading and writing files on disks are provided by the operating system. Modern Operating systems do not allow common programs to directly operate on disks, reading and writing a file is to request the operating system to open a file object (usually called a file descriptor), and then read data (read a file) from the file object through the interface provided by the operating system ), or write data to this file object (Write File ).
Read files
To open a file object in Read mode, use the Python built-in open () function to pass in the file name and identifier:
?
1 |
>>> F = open ('/Users/michael/test.txt', 'R ') |
The identifier 'R' indicates read. In this way, a file is successfully opened.
If the file does not exist, the open () function will throw an IOError and provide the error code and detailed information to tell you that the file does not exist:
?
1 2 3 4 |
>>> F = open ('/Users/michael/notfound.txt', 'R ') Traceback (most recent call last ): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: '/Users/michael/notfound.txt' |
If the file is successfully opened, you can call the read () method to read all the content of the file at a time. Python reads the content to the memory, represented by a str object:
?
1 2 |
>>> F. read () 'Hello, world! ' |
The last step is to call the close () method to close the file. After the file is used, it must be closed because the file object occupies the resources of the operating system and the number of files that can be opened at the same time in the operating system is limited:
?
An IOError may occur during file read/write operations. If an error occurs, the subsequent f. close () will not be called. Therefore, to ensure that the file can be properly closed no matter whether an error occurs, try... finally:
?
1 2 3 4 5 6 |
Try: F = open ('/path/to/file', 'R ') Print f. read () Finally: If f: F. close () |
However, every write is too cumbersome. Therefore, Python introduces the with statement to automatically help us call the close () method:
?
1 2 |
With open ('/path/to/file', 'R') as f: Print f. read () |
This is the same as the previous try... finally, but the code is more concise and does not have to call the f. close () method.
Calling read () will read all the content of the file at a time. If the file has 10 Gb, the memory will pop up. Therefore, you can call the read (size) method repeatedly to ensure security, read a maximum of size bytes each time. In addition, you can call readline () to read a row of content at a time, call readlines () to read all the content at a time, and return the list by row. Therefore, you need to decide how to call it as needed.
If the file is small, read () is the most convenient one-time read. If the file size cannot be determined, it is safer to call read (size) repeatedly. If it is a configuration file, it is the most convenient to call readlines:
?
1 2 |
For line in f. readlines (): Print (line. strip () # Delete '\ n' at the end |
File-like Object
An Object with the read () method returned by the open () function is called file-like Object in Python. In addition to file, it can also be a memory byte stream, network stream, custom stream, and so on. File-like Object does not need to inherit from a specific class. You only need to write a read () method.
StringIO is the file-like Object created in the memory, which is often used as a temporary buffer.
Binary files
By default, all text files are read and ASCII-encoded. To read binary files, such as video and video, open the file in 'rb' mode:
?
1 2 3 |
>>> F = open ('/Users/michael/test.jpg', 'rb ') >>> F. read () '\ Xff \ xd8 \ xff \ xe1 \ x00 \ x18Exif \ x00 \ x00...' # hexadecimal bytes |
Character encoding
To read a non-ASCII text file, it must be opened in binary mode and decoded. For example, GBK-encoded files:
?
1 2 3 4 5 |
>>> F = open ('/Users/michael/gbk.txt', 'rb ') >>> U = f. read (). decode ('gbk ') >>> U U' \ u6d4b \ u8bd5' >>> Print u |
Test
It would be too troublesome to manually convert the code every time (it is a good thing to write code that is long, hard to understand and cannot be maintained if you are afraid of trouble ), python also provides a codecs module to automatically convert the encoding when reading files and read unicode directly:
?
1 2 3 |
Import codecs With codecs. open ('/Users/michael/gbk.txt', 'R', 'gbk') as f: F. read () # U' \ u6d4b \ u8bd5' |
Write files
The only difference between writing a file and reading a file is that when the open () function is called, the input identifier 'W' or 'wb 'indicates writing a text file or writing a binary file:
?
1 2 3 |
>>> F = open ('/Users/michael/test.txt', 'w ') >>> F. write ('hello, world! ') >>> F. close () |
You can call write () repeatedly to write the file, but you must call f. close () to close the file. When writing a file, the operating system usually does not immediately write data to the disk, but caches the data in the memory and writes the data slowly when it is idle. Only when the close () method is called can the operating system ensure that all data not written to the disk be written. The consequence of failing to call close () is that the data may only be written to the disk, and the rest will be lost. Therefore, the with statement is used for insurance:
?
1 2 |
With open ('/Users/michael/test.txt', 'w') as f: F. write ('hello, world! ') |
To write a text file of a specific encoding, follow the example of codecs to write unicode, which is automatically converted from codecs to the specified encoding.
Summary
In Python, File Reading and Writing is completed by opening file objects through the open () function. It is a good habit to use the with statement to operate on file IO.