Eighth Quarter
- Read and write files
Mode |
Describe |
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. |
Rb |
Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. |
r+ |
Open a file for read-write. The file pointer will be placed at the beginning of the file. |
rb+ |
Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. |
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. |
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. |
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. |
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. |
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. |
The open () method opens the
The Write () method is used to write
The. Read () method is used to read . ReadLine (). ReadLines ()
F=open ()
For line in F:
Print (line,end= "")
B.close ()
From Urllib Import Request
Response
- file read/write open (Filenae,mode)
Read () all file contents,write () writes, ReadLine () reads a line, ReadLines reads a list
Tell () gets the current location of the file
Seek (n,0) positioning pointer, 0: Moving from the beginning 1: Moving from the current position 2: Starting at the end this method only moves the binary
Close ()
- Loop Read file: (1) with open (Filename,mod) as F:f.read ()
(2) F=open () for line on F:print (line) Close ()
3.from urllib Import Request Crawl page content
F=request.urlopen ("url") ff=open (, ' w+ ')
Ff.write (str (respone.read ())) Close ()
4.pickle Serialization and deserialization
Open ()
Obj=pickle.dump (Obj,file)
Close ()
Inverse sequence
Open ()
Obj=pickle.load (file)
Close ()
Python section Eighth