Python-File Read and write

Source: Internet
Author: User

R: Read-only mode (default to read-only mode if no mode is specified)

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' R ')  # file Handle fr = F.read () print (FR) f.cl OSE ()  # Close File

Run results

If you perform a continuous read () operation on the same file handle, only the first read () operation has content and the rest is blank.

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' R ')  # file Handle fr = F.read () FR1 = F.read ( ) FR2 = F.read () print (FR) print ("---------") print (FR1) print ("+++++++++") print ("---------") print (FR2) print ("+++++++ + + ") F.close ()  # Close File

Run results

Because the read () operation is sequential

ReadLine () reads in a row

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' R ')  # file handle print (F.readline ()) Print (F.readline ()) print (F.readline ()) print (F.readline ()) print (F.readline ()) F.close ()  # Close File

Run results

Because there is a line break at the end of each line \ n, when you print it, it prints, so it's empty.

ReadLines () reads the contents of the file in the behavior unit

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' R ')  # file Handle fr = F.readlines () print (FR ) F.close ()  # Close File

Run results

The result is a list, with each line string being an element of that list, with a newline character at the end of each line \ n

Print the contents of each line

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' R ')  # file handle for lines in F.readlines ():    print (Line.strip ())  # Strip () Remove line break F.close ()  # Close File

Run results

ReadLines () compared to memory when large files are encountered, because ReadLines () reads the entire file directly into memory, it is recommended to use the following method

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' R ')  # file handle for lines in F:    print (l Ine.strip ())  # Strip () Remove line break F.close ()  # Close File

This method is read in a row

Readable () Determine if the file is readable

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' R ')  # file Handle fr = F.readable () print (FR) F.close ()  # Close File

Run results

W: Write-only mode, file does not exist then create, file exists overwrite original file

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_w ', mode= ' W ') F.write (' Benjamin ") f.write (' Li gentry \ n ') f.write (' Spring planting a grain of millet, harvest million son. \ n ') f.write (' No Shida in the seas, the farmer starved to death. ') F.close ()

Run, created a file "File_w" (local without the file)

If the file exists,

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_w ', mode= ' W ') f.write (' Test ') f.close ()

Run results

The previous content was overwritten with new content

There is also a writelines () method that can write a sequence

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_w ', mode= ' W ') f.writelines (["Spring planting a grain of millet,", "Autumn Harvest million sons.") \ n "]) F.writelines (" The Sihai is not Shida, "" The farmer starved to death. ") \ n ")) F.writelines ({" Hoe wo Day Copse, "," Sweat wo xia Tu. ") \ n "}) F.writelines (" Who knows the dishes, the grain is hard. ") F.close ()

Run results

Concatenation of individual elements separated by commas

A: Append mode, can only write unreadable, will not create a new file, will not overwrite the original file, append content after the original file

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= "File_w", mode= ' a ') f.write (' \nthis is a test ') f.close ()

Run results

Append mode is written in the same way as read-only mode, except that append mode adds content at the end of the original file

RB: Read-only binary mode

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' rb ') FR = F.read () print (FR) f.close ()

Run results

WB: Write-only binary mode

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_w ', mode= ' WB ') f.write (' Spring planting a grain of millet, harvest million son. \ n '. Encode ()) F.write (b ' This is a test ') f.close ()

Run results

WB can also be used to write image files, audio files, etc.

AB: Append binary mode

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= "File_w", mode= ' ab ') f.write (' \nthis is from append1\n '. Enco De ()) F.write (b ' This was from Append2 ') F.close ()

Run results

r+: Read/write mode, both readable and writable (read mode support write)

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_test ', mode= ' r+ ') fr = F.read () print (FR) f.write (' from Writing ') FR1 = F.read () print (FR1) f.close ()

Run results

What to write

The words that were written in later were not read.

w+: Read-write mode, both writable and readable (write mode supports reading)

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= ' file_w ', mode= ' w+ ') print (F.read ()) f.write (' Sihai no Shida, ') F.write (' The farmer is starving. ') print (F.read ()) F.close ()

Run results

The contents of the read are empty.

What to write

The original content was overwritten.

A +: readable append mode

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" f = open (file= "File_w", mode= ' A + ') print (F.read ()) f.write (' \nthis ' from Append1\n ') F.write (' This was from Append2 ') print (F.read ()) F.close ()

Run results

What to write

Python-File Read and write

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.