Python Learning Notes (23) file read/write

Source: Internet
Author: User

Excerpt from: Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431917715991ef1ebc19d15a4afdace1169a464eecc2000

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 read- open() file mode , use Python's built-in 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:

   >>> F=open ( " /users/michael/notfound.txt  ,  r   "    , 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 f.close() error, 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  

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 method repeatedly read(size) , 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 inch f.readlines ():     Print # Delete the "\ n" at the end.
File-like Object

Like the open() function returned by this kind read() of object with a method, in Python is called file-likeobject. 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.

StringIOFile-like Object, which is created in memory, is commonly 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, ‘rb‘ open the file in a 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 open() encoding parameters to the function, for example, to read the GBK encoded file:

  >>> f = open ( " /users/michael/gbk.txt  , "  Span style= "color: #800000;" >r  , Encoding="  GBK   " )  >>> F.read ()   "  test   '    

If you encounter some encoded files, you may encounter unicodedecodeerror because there may be some characters that are illegally encoded in the text file. In this case, open () function also receives a errors parameter, which represents < Span style= "FONT-SIZE:18PX;" > If you encounter a coding error, how to handle . 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 open() when a function is called ‘w‘ , ‘wb‘ An identifier is passed in or a write-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 a specific encoded text file, pass in the parameter to the open() function encoding and automatically convert the character 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 Learning Notes (23) file read/write

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.