Python 3.2 Official document translation of file read and write

Source: Internet
Author: User
Tags empty readable readline

5.2 File reading and writing

The open () method returns a file object, which in most cases passes two objects: open (filename, mode);

For example:

>>> f = open ('/tmp/workfile ', ' W ')

The first argument is a string containing the file name, and the second is a string that contains how the file is used. If the file is read-only marked "R", write-only marked "W" (an existing file with the same name will be purged), and "a" is added to the end of the file, and the data is automatically added to the end of the file. "r+" means readable and writable. The mode parameter is optional and, if there is no such parameter, the default is readable.

Normally, the file opens in text mode, which means you can read and write the string to the file, and you can set the encoding format for reading and writing (the default encoding format is Utf-8). ' B ' means opening the file in binary mode. This data will be read and written in Byte object form. This pattern can be used in all non-text format files.

This is a harmless file modification, but can corrupt binary data in a JPEG or EXE file. So it is best to use binary format when reading and writing such files.

5.2.1 the method in the File object

In all of the examples in this section, assume that a file named F has been created.

To read the contents of the file, call F.read (size). This method can read a certain size of data and return a string or byte object. The size is a selectable numeric parameter. When size is missing or negative, the entire file content is returned. If the file content is twice times the size of your memory, that's your problem. However, you should read the contents of the file as large as possible, and if you have reached the end of the file, F.read () will return an empty string ("),

>>> F.read ()

' This is the entire file.\n '

>>> F.read ()

’’

F.readline () reads a single line from a file, and the end of the character takes a new character, "\ n", and is omitted only if the last line of the file does not have a newline character. If F.readline () returns an empty string, it indicates that the end of the file has been reached. When an empty row is represented by "\ n", a string containing only a newline character is returned.

>>> F.readline ()

' This is the ' file.\n '

>>> F.readline ()

' Second line of the ' file\n '

>>> F.readline ()

‘’

F.readlines () Returns a list containing multiple rows of data in a file. If you pass in a selectable sizehint, it reads the complete line containing at least the specified number of bytes and returns it from the file. This method is often used to efficiently read data from large files in rows, without loading the entire contents of the file into memory. This method returns only the complete file row.

>>> F.readlines ()

[' Is the ' file.\n ', ' Second line of the ' file\n ']

Another way to choose to read multiple rows is to loop the file object. This is a way of memory that is efficient, fast, and code-concise.

>>> for line in F:

... print (line, end= ')

...

This is the the file.

Second Line of the file

Although the latter method is concise but does not provide more detail control ability, because two methods manage different row cache, do not confuse.

F.write (String) writes the byte content to a file, returning the number of characters written to the file.

>>> F.write (' is a test\n ')

15

Other content can be written in addition to the write string, but must first be converted to a string.

>>> value = (' The answer ', 42)

>>> s = str (value)

>>> F.write (s)

18

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.