1. Open File:
Open function
The most common use of the Open function is as follows: File handle = Open (' File path ', ' mode ', encoding method). Encode= "
1. About file paths
#文件路径:
There are two main types, one is to use relative paths, and the above example is to use relative paths.
The other is an absolute path, such as: C:/users/desktop/python/test.txt '
2. Read the file:
R |
Open the file as read-only . The pointer to the file will be placed at the beginning of the file. This is the default mode. (read-only mode) pointer at the beginning |
Rb |
Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. generally used for non-text files and so on . (read-only mode) pointer at the beginning |
r+ |
Open a file for read-write . The file pointer will be placed at the beginning of the file . (read-write mode) pointer at the beginning |
rb+ |
Opens a file in binary format for read- write . The file pointer will be placed at the beginning of the file. generally used for non-text files and so on .
|
W |
Open a file for writing only . If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted. If the file does not exist, create a new file. (Delete the original content, write, the file does not exist on the Create) pointer at the beginning |
Wb |
Open a file in binary format only for writing . If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted . If the file does not exist, create a new file. Generally used for non-text files and so on. |
w+ |
Open a file for read-write . If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted . If the file does not exist, create a new file. pointer at the beginning |
wb+ |
opens a file in binary format for read-write . If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted. If the file does not exist, create a new file. Generally used for non-text files and so on. |
A |
Opens a file for appending . If the file already exists, the file pointer will be placed at the end of the file . In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
Ab |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
A + |
Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write. |
ab+ |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write. |
# test.txt Original: Python,is,on,the,way
# read-only
F=open (' Test.txt ', ' R ')
Print (F.read ()) # output python,is,on,the,way# read/write
#读写
# First Write againF=open ('Test.txt','r+')f.write ( 'java') Print(F.read ())#On,is,on, the, thef.close ()text content:#Javaon,is,on,the,wayFirst, opening the cursor in the R mode will be at the very beginning, at which point the f.write is executed ('Java'), the original Pyth is immediately followed by print (F.read ()). Will print out the rest of the text that follows. So the output is: on, is, ON, the, the whole process is complete, the entire text content becomes Javaon, is, On,the,way (cursor!! The cursor written by the input string to move backwards, the space, commas and other symbols are counted! Be superseded
Python,is,on,the,way
When writing to java1234
In the final text, theJava1234s,on,the,way
#----------------------------------------------
#先读再写
F=open (' test.txt ', ' r+ ')
F.read ()
F.write ("java1234")
F.write ("AA")
F.write ("BB")
F.write ("CC")
F.write ("DD")
Read-Before-write results: PYTHON,IS,ON,THE,WAYJAVA1234AABBCCDD
The first reading and then writing is to read the following, then add, first write again in the beginning of the text is added.
= Open ("text.txt",'r', encoding="utf-8 " ) for in F: print(line) F.close () is the content of the output file
3. Close the file:
It can also cause other unsafe hazards. There is also a way to let us not deliberately focus on closing the file. That's with open ()
With open ('test.txt','a+') as F: f.write (' 123') print(f.readable ())
# ----------------------------------------------------------------------- Three operations for reading files 1, read () # reads all the contents of the text at once, returning the result as a string
It is characterized by reading the entire file and putting the contents of the file into a string variable. The disadvantage is that you cannot use the Read () method if the file is very large, especially if it is larger than memory. Read () reads the bytes directly into the string, including the line break
2, ReadLine () # reads only the contents of the first row at a time, returning the result as a string 3, ReadLines () # reads all the contents of the text and returns the result in the form of a list , typically with the for in
#--------------------------------------------------------------------------------
Text = File.read () # result for STR type
Read (),. ReadLine (), and. ReadLines (). Each method can accept a variable to limit the amount of data that is read each time, but they typically do not use variables.
# Write
Python Write file Write (string), Writelines (list)
# Test.txt Original: Python,is,on,the,way
F=open (' test.txt ', ' r+ ')
Print (F.read ())
F.write (R ' This\nis\nhaiku ') #write (String)
Print (F.read ()) >> output
Python,is,on,the,way
This\nis\nhaiku
# don't add R
Output:
Python,is,on,the,way
This
Is
Haiku
There is also the write, Writelines method, which is similar to the method described above, except that write writes the object when the string (str), Writelines writes the list, namely: Obj.write (str) obj.writelines ( Sequence
# Writelines (list)
list02 = ["One", "Test", "hehe", "44", "55"]
Obj.writelines (list02)
Iv. Common mode value combinations
1, R or RT default mode, text mode read
2, RB binary files
3, w or wt text mode write, before opening the file storage is emptied
4, WB binary write, file storage is also emptied
5, a append mode, can only be written at the end of the file
6, A + read-write mode, write can only be written at the end of the file
7, w+ can read and write, and A + is the difference is to clear the contents of the file
8, r+ can read and write, and A + difference is can write to the file anywhere
Python file processing