First, the document processing process
1. Open the file, get the file handle and assign a value to a variable
2. Manipulating files with a handle
3. Close the file
Second, the basic operation
1. File Open mode
File handle =open (' File path ', ' mode ')
When you open a file, you specify the file path and how to open the file.
The mode of opening the file is:
- R, read-only mode "default mode, file must exist, not present, throw exception"
- W, write-only mode "unreadable; not exist" created; empty content "
- X, write-only mode "unreadable; not present, create, present error"
- A, append mode "readable; not present, create; append content only"
##只读模式#F=open (R ' c.txt ', encoding= ' utf-8 ')## print (' ====>1 ', F.read ())## print (' ====>2 ', F.read ())## Print (f.readable ())## Print (F.readline (), end= ")## Print (F.readline ())## print ("=" *20)## Print (F.read ())#print (F.readlines ())#f.close ()#Write mode: The file does not exist, it is created, the file exists, overwriting the original#F=open ("new.py", ' W ', encoding= ' utf-8 ')#f.write (' 1111111111\n ')#f.writelines ([' 2222\n ', ' 2222548\n ', ' 978646\n '])#f.close ()#Append mode: File does not exist then create, file exists will not overwrite, write content is appended way write#f=open (' new.py ', ' a ', encoding= ' utf-8 ')#f.write (' nishishui\n ')#f.writelines ([' aa\n ', ' bb\n '])#f.close ()View Code
"+" means you can read and write a file at the same time
- r+, read and write "readable, writable"
- w+, write "readable, writable"
- x+, write "readable, writable"
- A +, write "readable, writable"
"B" means to operate in bytes
- RB or R+b
- WB or W+b
- XB or W+b
- AB or A+b
Note: When opened in B, the content read is a byte type, and a byte type is required for writing, and encoding cannot be specified
Practice, using B-mode, write a CP tool that requires the following:
1. Can copy text and copy video, pictures and other files
1 #b Mode2F=open ('1.jpg','RB')3Data=F.read ()4 #print (data)5F=open ('2.jpg','WB')6 f.write (data)7 Print(data)View Code
Third, context management
1. With open (' A.txt ', ' W ') as F:
Pass
2.with open (' A.txt ', ' R ') as Read_f,open (' B.txt ', ' W ') as Write_f:
Data=read_f.read ()
Write_f.write (data)
Iv. Modification of documents
1 ImportOS2With open ('a.txt','R', encoding='Utf-8') as Read_f,3Open'A.txt.swap','W', encoding='Utf-8') as Write_f:4 forLineinchRead_f:5 Write_f.write (line)6 7Os.remove ('a.txt')8Os.rename ('. A.txt.swap','a.txt')View Code
Five, the cursor movement within the file
One: Read (3):
1. When the file is opened in text mode, the representation reads 3 characters
2. When the file is opened in B mode, the delegate reads 3 bytes
Second: The rest of the files within the cursor movement are in bytes such as Seek,tell,truncate
Attention:
1. Seek has three modes of movement 0,1,2, of which 1 and 2 must be performed in B mode, but regardless of which mode is moved in bytes units
2. Truncate is to truncate the file, so the file must be opened in a way that can be written, but cannot be opened with W or w+, because that will directly empty the file, so truncate to r+ or a or a + and other modes to test the effect
1 Import Time2With open ('Test.txt','RB') as F:3F.seek (0,2)4 whileTrue:5Line=F.readline ()6 ifLine :7 Print(Line.decode ('Utf-8'))8 Else:9Time.sleep (0.2)View Code
Python----------file operations