Python section Eighth

Source: Internet
Author: User

Eighth Quarter

    1. 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

    1. 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 ()

    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.