How does file read/write IO in QuickStart python work with external data?

Source: Internet
Author: User

Read-write files are the most common IO operations. 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 a read-file mode, use the Python built-in open () function 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 a IOError error and gives the error code and detailed information telling you that the file does not exist:

>>> f=open ('/users/michael/notfound.txt ', ' R ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Filenotfounderror: [Errno 2] No such file or directory: '/users/michael/notfound.txt '

If the file opens successfully, the next call to the Read () method reads the entire contents of the file at once, and Python reads the contents into memory, denoted by a str object:

>>> 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 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 ()

Because the file reads and writes the possibility to produce the ioerror, once the error, the later F.close () will not call. So, to ensure that the file is closed correctly, whether or not it is an error, we can use the WITH statement to automatically call the close () method for us:

With open ('/path/to/file ', ' R ') as F:
Print (F.read ())

We can also use the try ... finally to implement, but the code is cumbersome, with the code is better concise, and do not have to call the F.close () method.

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

If the file is small, read () One-time reading is the most convenient, if the file size cannot be determined, repeated calls to read (size) insurance, if it is a configuration file, call ReadLines () the most convenient:

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

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 ' RB ' mode:

>>> f = open ('/users/michael/test.jpg ', ' RB ')

>>>f.read () b ' \xff\xd8\xff\xe1\x00\x18exif\x00\x00 ... ' # Hexadecimal representation of bytes

Character encoding

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

>>> f = open ('/users/michael/gbk.txt ', ' R ', encoding= ' GBK ')

>>> f.read () ' Test '

If you encounter some code that is not canonical, you may encounter unicodedecodeerror, because there may be some illegal encoded characters in the text file. 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 just suddenly

Slightly:

>>> f = open ('/users/michael/gbk.txt ', ' R ', encoding= ' GBK ', errors= ' ignore ')

Write a file

The only difference between writing a file and reading a file is that when the open () function is called, the incoming identifier ' W ' or ' WB ' represents a text file or a write binary:

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

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

>>> F.close ()

You can write the file repeatedly by calling write (), 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 close () method is called does the operating system guarantee that all data that is not written is written to disk. The consequence of forgetting to call Close () is that the data may have been written only partially to the disk, and the remainder has been lost. Therefore, it is safe to use the WITH statement:

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

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

Summary

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

How does file read/write IO in QuickStart python work with external data?

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.