Full-stack Python development: python File Processing and python File Processing
Python File Processing
Open File Mode
Common file functions
1. open the file and close the file
Procedure:
#1. open the file, get the file handle, and assign it to a variable #2. Operate the file through the handle #3. close the file
Open () method: returns a file object.
Syntax format:
# File = open (filename, mode, encoding) # open the file # Read and Write the file operation # file. close () # close the file
Common Parameters
Filename: Name of the file to be opened (file path)
Mode: File opening mode (SEE)
Encoding: Encoding format of open files
Ii. file Read operations (text)
Prerequisites: The Read File a.txt already exists.
A.txt File Content
This readable file 11 this readable file 22 This readable File 33
F. read () reads all characters in the file.
# Open the file (if a.txt exists) f = open('a.txt ', mode = 'R', encoding = 'utf-8') print (f. read ())
F. close ()
F. readline () reads a row from a file.
f = open('a.txt',mode='r',encoding='utf-8')print(f.readline())
f.close()
F. readlines () reads all rows in the file and returns a list of rows as a single element.
f = open('a.txt',mode='r',encoding='utf-8')print(f.readlines())
f.close()
Loop read
f = open('a.txt',mode='r',encoding='utf-8')for i in f: print(i) f.close()
Pointer
f = open('a.txt',mode='r',encoding='utf-8')print(f.readline(),end='')print(f.readline(),end='')print(f.readline(),end='')print(f.readline(),end='')print(f.readline(),end='')f.close()
From the above results, we can see that we read a total of five rows, only three rows are displayed. Because there is a pointer in the file reading process, where the file is read, the pointer will point to where it is, after reading three lines, the pointer is already at the end of the file, at this time, there will be no content in the file read operation.
Conclusion:The pointer indicates where it is read, and the pointer is at the end. No content can be read.
Summary:
R: Default open mode, read-only mode
Note: When the file does not exist, an error is returned.
Iii. file write operation (text) Write-only mode (w)
F. write ()
F. writelines ()
F=open(r'a1.txt ', mode = 'w', encoding = 'utf-8') # The default value is wtf. write ('first line \ n') f. write ('second line \ n') f. writelines (['2014 \ n', '2014 \ n', '2014 \ n']) # Write string list f to the file. write ('aaaaa \ nbbbb \ nccccc \ n ')
Summary:
W: Write-only mode
1. When the file exists, clear it
2. Create an empty document when the file does not exist
Append mode ()
# A: only append write mode # Note: # create an empty file when the file does not exist # When the file exists, the cursor directly runs to the end of the file f = open ('access. log', mode = 'A', encoding = 'utf-8') # print (f. writable () # f. readlines () # error f. write ('2014 \ n') f. close ()
Summary:
A: Only append write mode
1. When the file exists, do not empty it. Add it at the end
2. When the file does not exist, create an empty document and add it at the end
Iv. file Read operations (Binary) 5. file write operations (Binary)
To be supplemented