SoFunction
Updated on 2024-11-12

Python basics of file manipulation

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!