General contents of file operations.
# Manipulation of documents # open file open Open an existing file or create a new file open('./','w')
# Document manipulation # open file open Open an existing file or create a new one fobj=open('./','w') # Pass it to an object to manipulate it. # Start operation Read/write operation ("On a vast sea, a flock of seabirds.) () #Save plus close
# Manipulation of documents # open file open Open an existing file or create a new file fobj=open('./','w') # Pass it to an object to manipulate it. # Start operation Read/write operation # ('On a vast sea there's a flock of seabirds') ('Breaking through the wind and waves') # The file exists, it will be overwritten () #Save plus close
# Manipulation of documents # open file open Open an existing file or create a new file # The default encoding is gbk Chinese encoding The best habit is to specify an encoding when we open a file # Give him a code type fobj=open('./','w',encoding='utf-8') # Pass it to an object to manipulate it. # Start operation Read/write operation ("On a vast sea, a flock of seabirds.) ('Breaking through the wind and waves') # The file exists, it will be overwritten () #Save plus close # Write the data in binary form # fobj=open('','wb') #str——>bytes ('No more canals between the sea'.encode('utf-8')) ()
fobj=open('./','w') ("On a vast sea, a flock of seabirds.) ('Breaking through the wind and waves') # The file exists, it will be overwritten fobj=open('','a') # Additional ('Between the clouds and the sea') ()
# Read data operations f=open('','r') # print(()) # Read it all from start to finish print((10)) # Specify the number of reads print(()) # The first line is read with parameters, the rest of the lines are outputted
f=open('','rb') data=() print(data) print(('gbk')) () #File objects need to be closed
# with context management objects # Benefits Automatic release of objects with open associations with open('','r') as f: print(())
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!