The open () function in Python specifies how the file is opened

Source: Internet
Author: User
how files are opened

There are several open modes when we use the open () function for opening a file.

Read-only, ' R '
' W '-write only, file already exists then empty, not exist then create.
' A ', append, write to end of file
' B ', binary mode, such as open images, audio, Word files.
' + ' update (readable and writable)

This with the ' + ' is a bit difficult to understand, on the code to feel under.

With open (' foo.txt ', ' w+ ') as F:   f.write (' bar\n ')   f.seek (0)    data = F.read ()

You can see that the above code, which can not only write, but also read out. Note that you should first navigate to the beginning, F.seek (0), or read out the empty data.
Some people may be confused, since the band ' + ' is readable and writable, what is the difference between ' w+ ' and ' r+ '.
That is,
' w+ ' will be emptied and will be created (the file already exists then emptied and does not exist.) )
' r+ ' does not empty, does not create

Do not open text files in binary mode
Let's look at the "weird" behavior of the code below.
Assuming that I have a f.txt file under Windows, the contents are as follows.

HelloWorld

Code One,

With open (' F.txt ', ' R ') as F:   print f.readlines () with open (' f.txt ', ' RB ') as F:   print f.readlines ()

Output

[' hello\n ', ' world\n '] [' hello\r\n ', ' world\r\n ']

Code two,

With open (' f.txt ', ' RB ') as f:   data = F.read () with open (' F.txt ', ' W ') as F:   f.write (data)

Open the file and turn it into the following,

Hello^mworld^m

First, we first understand the concept of the newline character ' \ n ' followed by the carriage return ' \ R '.
' \ n ', newline (lf,line-feed), refers to a new line.
' \ r ', carriage return (Cr,carriage-return), referring back to the wardrobe.

Because the line-break identities are not the same on different systems.

Windows-> ' \ r \ n ' unix-> ' \ n ' mac-> ' \ R '

This is why txt under windows will have ' ^m ' at the end of Linux when it is opened.
That's why I ran the script under Linux to export the game data to a local windows open into a row.

In fact, the text file is also a binary file, is a text encoded binary files, text files on some invisible characters are processed to increase readability.

In Python, you can obtain a newline identifier for the current system through OS.LINESEP. For example, under Windows, Os.linesep is ' \ r \ n '.
In Python, when you operate a newline identifier, and do not control what platform, the direct use of ' \ n ' on the line, Python will automatically according to the different systems into different identities.

With the above theories, we can parse the "strange" phenomenon of the code at the beginning of this article.
In code one, a file opened in text mode, the newline identifier is processed by Python as ' \ n ', and binary mode is left intact.
Code two, opened in binary mode, written in text mode. Binary open intact or ' \ r \ n ', while the text mode is written because Python will turn ' \ n ' to ' \ r \ n ', so in fact it is written ' \r\r\n ', so there is a ' ^m '.

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.