01Python basics _06 File reading and writing



  1. Read the file

  Use the open function or the file function to read the file, using the string of the file name as the input parameter

 

  1 #Read the file content

2

3 f = open('test.txt')

4

5 print(f.read( )) #Read the entire contents of the file

6 print(f.readline()) #Read the first line

7 of the file print(f.readlines()) #Return a list, each element represents a line

8

9 f.close() #Close the file

 

  

Method

    description

   read()

    reads all the contents of the file at a time, returns a str

   read(size)

    reads the content of the specified length at most each time, and returns a str; in Python2, size specifies the length in bytes, in Python3 size specifies the character length

   readlines()

    reads all the contents of the file at one time, and returns a list by line

   readline()

    reads only one line at a time and

     traverses each line in the print file:

 

  1 with open('song.txt', 'r ', encoding='utf-8') as f:

2 for line in f.readlines():

3 print(line)

 

  or:

 

  1 with open('song.txt', 'r', encoding='utf-8', newline='') as f:

2 for line in f:

3 print(line)

 

  2. To write a file

  open() opens the file in r mode by default. To write, you need to open the file in w mode. When opened in w mode, the file is created if it does not exist, and the previous content is overwritten if the file exists.

 

  1 #Write the file

2

3 f = open('test.txt', ' http://www.cppentry.com Programming Development Programmer Getting Started>w') #Use w mode to open the file

4 f.write('hello world!') #Write content

5 f.close() #Close the file

 

File open mode

    description

   r

    Open the file in read-only mode, and point the file pointer to the file header; if the file does not exist, an error will be reported

   w

    Open the file in write-only mode , and point the file pointer to the file header; clear the contents of the file if it exists, and create

   a if the file does not exist

    Open the file in append-only writable mode, and point the file pointer to the end of the file; if the file does not exist, create

   r +

    add the writable function on the basis of r +     add the readable function on the basis of w

   a    +     on the basis of a The readable function    b is added to read and     write binary files (the default is t, which means text), which needs to be used in conjunction with the above modes, such as ab, wb, ab, ab+ (POSIX systems, including Linux, will ignore this character)      3. Closing the file   Closing the file ensures that the contents have been written to the file, and not closing may have unexpected results. Use close() to close the file.   You can use try...finally to ensure that the file can be closed anyway:   1 #write the file 2 try: 3 f = open('test.txt', 'w+') #open the file using w+ mode 4 f.write ('hello world!') #Write content 5 finally: 6 if f: 7 f.close() 8 print('file has been closed. ')   In fact, Python provides a safer method when the with block After the content is over, Python will automatically call its close method to ensure the safety of reading and writing:

















 

















 



 

  1 #Write to file

2 with open('test.txt', 'w') as f:

3 f.write('hello world!') #Write content

4 print(f.closed) #Return True



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397211&siteId=291194637