Python crawler--store Local

Source: Internet
Author: User

1. Save to TXT

title="today is beautiful"withopen ('c:\\users\\leon\\desktop\\ Title.txt',"a+") as F:    F.write (title)    F.close ()

Create a title.txt text on the desktop,

A + is read-write mode, and if the file does not exist, a file is created.

2. Read and write the data in txt

With open ('c:\\users\\leon\\desktop\\title.txt'"R", encoding ='utf-8') as f:    = f.read ()    print ( Result

The results are: today is beautiful

How to open a file centrally:

R: Open File as read-only, error if file does not exist
W: Open the file as write-only, the file exists or empty, and does not exist.
A: Open in Append-only mode, do not empty the file, add content at the end of the file
r only Read permission, W and a only write permission, W empty the file, a does not empty the file. (read, write,append)
The following code says the difference between the name r+,w+,a+:

FD = open ("1.txt",'w+') Fd.write ('123')#move file pointer to firstFd.seek (0,0)Print("w+ mode open:"+ fd.read () +"#读写权限打开文件, the file is emptied as soon as it is opened, the file does not exist, the file is created")#Output 123fd.close () FD= Open ("1.txt",'r+') Fd.write ('456') Fd.seek (0,0)Print("r+ mode open:"+ fd.read () +"#读写权限打开文件, if the data is written, the file is emptied and there is no error in the file")#output 456, 123 of the previous write is emptied.fd.close () FD= Open ("1.txt",'A +') Fd.write ('789') Fd.seek (0,0)Print("A + mode opens:"+ fd.read () +"#读写权限打开文件, do not empty files, add content to the end of file, file does not exist create file")#Output 456789Fd.close ()

Python crawler--store Local

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.