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