file handling

  

#Open the file windows default encoding gbk so you need to decode it with jbk 
f=open( ' hello.txt ' , encoding= ' utf8 ' ) # use gbk decoding operation 
data=f.read() # read the file content 
print (data ) #Print the file content 
f.close() #Close the file
# f=open('hello.txt','r',encoding='utf-8') 
# # data=f.read()#Read the file content 
# # print(data) 
# print(f.readable( ))#Display whether it is readable or not, if it is r, display true 
# print(f.readline())# Read the content of a line of open files 
# f.close() 
# r read w write r append 
# w no matter whether there is a document or not Clear the contents of the file and create a new document 
# f=open('hello.txt','w',encoding='utf-8') 
# f.write('123456\n')#The contents of the original file will be cleared directly Use this string instead of 
# #Append operation a 
# f=open('hello.txt','a',encoding='utf-8') 
# f.write('write to the end of the file')# can be achieved in the original Append new content to the file 
# r+ readable and writable 
# f=open('hello.txt','r+',encoding='utf-8')
#data=f.read() 
# print(data) 
# f.write('Who are you?\n') 
# data=f.read() 
# print(data) 
f.close()#The file must be closed or it will not work

with open( ' hello.txt ' , ' w ' ,encoding= ' gbk ' ) as sf.open( ' cat.txt ' , ' w ' ,encoding= ' gbk ' ) as f:#with no need to close the fileclose f.write('llsd\n')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325075341&siteId=291194637