[Python] Python open read/write files

Source: Internet
Author: User
1. Open

After opening a file using open, remember to call the close () method of the file object. For example, you can use the try/finally statement to ensure that the file can be closed at last.
File_object = open('thefile.txt ')
Try:
All_the_text = file_object.read (
)
Finally:
File_object.close ()


Note: The open statement cannot be placed in the try block, because when an exception occurs when the file is opened, the file object file_object cannot execute the close () method.

2. Read the text file input = open ('data', 'R ')
# The second parameter defaults to R.
Input = open ('data ')



Read Binary file input = open ('data', 'rb ')



Read all content file_object = open('thefile.txt ')
Try:
All_the_text = file_object.read (
)
Finally:
File_object.close ()



Read the fixed byte file_object = open ('abinfile', 'rb ')
Try:
While true:
Chunk = file_object.read (100)
If not Chunk:
Break
Do_something_with (chunk)
Finally:
File_object.close ()



Read the list_of_all_the_lines = file_object.readlines (
)


If the file is a text file, you can directly traverse the file object to get each line:
For line in file_object:
Process Line



3. Write a file to write a text file output = open ('data', 'w ')



Write binary file output = open ('data', 'wb ')



Append Write File output = open ('data', 'W + ')



Write Data file_object = open('thefile.txt ', 'w ')
File_object.write (all_the_text)
File_object.close ()



Write multiple lines of file_object.writelines (list_of_text_strings)


Note that calling writelines to write multiple rows has a higher performance than using write for one-time writing.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~

The Ru or UA is opened in Read mode and supports common line breaks (PEP 278)
W    Open in write mode (clear if necessary)
A    Open in append mode (starting from EOF, create a file if necessary)
R +    Open in read/write mode
W +    Open in read/write mode (see W)
A +    Open in read/write mode (see)
RB    Open in binary read mode
WB    Open in binary write mode (see W)
AB    Open in binary append mode (see)
RB +   Open in binary read/write mode (see R +)
WB +   Open in binary read/write mode (see w +)
AB +   Open in binary read/write mode (see A +)

A.    Added in Python 2.3


Link: http://blog.sina.com.cn/s/blog_4ef8be9f0100gdax.html

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.