Example of using a Python file to append text content to an open read/write file

Source: Internet
Author: User
1. after open is used to open a file, remember to call the close () method of the file object. For example, you can use the tryfinally statement to ensure that the file can be closed at last. File_objectopen(thefile.txt) try: all_the_textfile_object.read () finally: file_object.close () Note: The open statement cannot be placed in the try block, because when the file is opened, the exception 1 occurs. 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 rinput = 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 each row 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.txt ', 'w ')
Write binary file output = open('data.txt ', 'WB ')
Append write file output = open('data.txt ', 'A ')

Output. write ("\ n are good guys") output. close ()


Write data file_object = open('thefile.txt ', 'w ')

file_object.write(all_the_text)file_object.close( )

The above is a detailed description of the details of the example of using Python files to operate open read/write files to append text content. For more information, see other related articles in the first PHP community!

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.