python--file read/write

Source: Internet
Author: User

File read/write

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 operate the disk directly, so the read-write file is to request the operating system to open a file object (often referred to as a file descriptor), and then, Read the data from this file object (read the file) from the interface provided by the operating system, or write the data to the file object (write file).

Read the file

To open a file object in read-file mode, use Python's built-in open() functions to pass in file names and identifiers:

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

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

If the file does not exist, the open() function throws an IOError error and gives the error code and detailed information to tell you that the file does not exist:

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, then the calling read() method can read the entire contents of the file at once, and Python reads the contents into memory, represented by an str object:

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

The final step is to call the close() method to close the file. The file must be closed after it is 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:

>>> f.close()

Since the file is read and written, it is possible to generate it, and IOError in the event of an error, f.close() it is not called. Therefore, in order to ensure that the file is closed correctly, whether or not it is an error, we can use it try ... finally to implement:

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 a with statement to help us invoke the close() method automatically:

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

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

The call read() reads the entire contents of the file one time, and if the file has 10G, the memory explodes, so, to be safe, you can call the read(size) method repeatedly, reading the contents of a size byte at most. In addition, the call readline() can read one line at a time, and the call readlines() reads all the contents once and returns by row list . Therefore, you need to decide how to call as needed.

If the file is small, one-time read() reading is the most convenient, if the file size can not be determined, repeated calls to read(size) compare insurance; If the configuration file, the call is readlines() most convenient:

for line in f.readlines():    print(line.strip()) # 把末尾的‘\n‘删掉
File-like Object

Like the open() function returned by this kind read() of object with a method, in Python is called File-like object. In addition to file, it can be memory byte stream, network stream, custom stream and so on. File-like object does not require inheritance from a particular class, just write a read() method.

StringIOIs the File-like Object created in memory, used as a temporary buffer

binary files

The default is to read the text file, and it is a UTF-8 encoded text file. To read a binary file, compare slices, videos, and so on, open the file in a ‘rb‘ mode:

>>> f = open(‘/Users/michael/test.jpg‘, ‘rb‘)>>> f.read()b‘\xff\xd8\xff\xe1\x00\x18Exif\x00\x00...‘ # 十六进制表示的字节
Character encoding

To read a text file that is not UTF-8 encoded, you need to pass in parameters to the open() function encoding , for example, to read the GBK encoded file:

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

If you encounter some code that is not canonical, you may encounter UnicodeDecodeError a number of characters that are illegally encoded in a text file that may be mixed. In this case, the open() function also receives a errors parameter that indicates what to do if a coding error is encountered. The simplest way is to ignore it directly:

>>> f = open(‘/Users/michael/gbk.txt‘, ‘r‘, encoding=‘gbk‘, errors=‘ignore‘)
Write a file

Writing and reading files are the same, the only difference being that when a function is called, an identifier is passed in or a write open() - ‘w‘ ‘wb‘ in file or a binary file is written:

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

You can write to write() the file repeatedly, but be sure to call f.close() to close the file. When we write a file, the operating system often does not immediately write the data to disk, but instead put it in memory cache, and then write slowly when idle. Only when the method is invoked close() does the operating system guarantee that all data that is not written is written to disk. The consequence of forgetting the call is that the close() data may have only been written to the disk, and the remainder has been lost. So, it is safe to use with statements:

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

To write to a specific encoded text file, pass in the parameter to the open() function encoding and automatically convert the string to the specified encoding.

Summary

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

python--file read/write

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.