A tutorial on file read and write operations in a Python program

Source: Internet
Author: User
Tags error code file size

This article mainly introduced in the Python program for file reading and writing operations tutorial, is the basic knowledge of Python learning, the need for friends can refer to the

Reading and writing files is the most common IO operation. Python has built-in functions for reading and writing files, and the usage is compatible with C.

Before reading and writing files, we must first understand that the ability to read and write files on disk is provided by the operating system, the modern operating system does not allow ordinary programs to manipulate the disk directly, so read and write files is to request the operating system to open a file object (usually called a file descriptor), and then, Read data (read files) from this file object through the interface provided by the operating system, or write the data to the file object (write file).

Read files

To open a file object in a read-file mode, use the Python built-in open () function to pass in the filename and designator:


1 >>> f = open ('/users/michael/test.txt ', ' R ')

The identifier ' R ' means read, so we successfully open a file.

If the file does not exist, the open () function throws a ioerror error, and the error code and detailed information 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, I n <module> ioerror: [Errno 2] No such file or directory: '/users/michael/notfound.txt '

If the file is opened successfully, Next, the Read () method is invoked to read the entire contents of the file at once, and Python reads the contents into memory, represented by a str object:


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

The final step is to call the close () method to shut down the file. The file must be closed after it has been used, because the file object consumes the resources of the operating system, and the number of files that the operating system can open at the same time is limited:


1 >>> F.close ()

The F.close () will not be invoked as soon as the file is read and written and the IOError is likely to occur. So, to ensure that the file is closed correctly, whether or not it goes wrong, we can use the try ... finally to implement:


1 2 3 4 5 6 try:f = open ('/path/to/file ', ' R ') print F.read () finally:if f:f.close ()

But it's so tedious to write every time, so Python introduces the WITH statement to automatically 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 better and simpler and does not have to invoke the F.close () method.

Calling read () reads the entire contents of the file at once, and if the file has 10G, the memory explodes, so, to be safe, you can repeatedly call the read (size) method to read up to the size byte at a time. In addition, the call to ReadLine () can read one line at a time and call ReadLines () to read all content at once and return to list by row. So decide how to call it as needed.

If the file is small, read () is the most convenient read (), and if you cannot determine the file size, call the read (size) to compare insurance; if it is a configuration file, it is most convenient to call ReadLines ():


1 2 For line in F.readlines (): Print (Line.strip ()) # Erase the end ' n '

File-like Object

This object, like the open () function, that has a read () method is called File-like object in Python. In addition to file, you can also be a byte stream of memory, network flow, custom flow, and so on. File-like object does not require inheriting from a particular class, just write a read () method.

Stringio is the File-like Object created in memory and is commonly used as a temporary buffer.

binary files

The previous default is to read the text file, and it is an ASCII-encoded text file. To read binary files, such as pictures, videos, and so on, open the file in ' RB ' mode:


1 2 3 >>> f = open ('/users/michael/test.jpg ', ' RB ') >>> f.read () ' xffxd8xffxe1x00x18exifx00x00 ... ' hexadecimal-represented Byte

Character encoding

To read a non-ASCII-encoded text file, you must open it in binary mode and then decode it. Like the GBK encoded file:


1 2 3 4 5 >>> f = open ('/users/michael/gbk.txt ', ' RB ') >>> U = F.read (). Decode (' GBK ') >>> u ' u6d4bu8bd5 ' >>> print U


If it's troublesome to manually convert code every time (writing a program for fear of trouble is a good thing, and will write long, difficult and maintainable code without fear of trouble), Python also provides a codecs module to help us automatically convert the code when we read the file and read the Unicode directly:


1 2 3 Import codecs with Codecs.open ('/users/michael/gbk.txt ', ' r ', ' GBK ') as F:f.read () # u ' u6d4bu8bd5 '

Write a file

Writing a file is the same as reading a file, except that when you invoke the open () function, passing in the identifier ' W ' or ' WB ' means 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 repeatedly call write () to write to the file, but be sure to call F.close () to close the file. When we write files, the operating system often does not write data to the disk immediately, but put it in memory cache, and then slowly write. Only when the close () method is invoked does the operating system ensure that all data not written is written to disk. The consequence of forgetting to call Close () is that the data may have only been written to disk, and the rest is lost. So, it's still insured with the WITH statement:


1 2 With open ('/users/michael/test.txt ', ' W ') as F:f.write (' Hello, world! ')

To write a specific encoded text file, follow the example of codecs and write to Unicode, which is automatically converted to the specified encoding by codecs.


In Python, file reads and writes are done through a file object opened by the open () function. Using the WITH statement to manipulate file IO is a good habit.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.