资料内容:
1、写文件
(1)写入字符串数据
# coding:utf-8
# 写入字符串数据
with open("write_example.txt", "w", encoding='utf-8') as file:
file.write("Hello, World!\n")
file.write("This is a new line.")
(2)写入字节数据
使用 write() 方法将字节数据写入文件。 可以使用 encode() 方法将字符串转换为字节数据进行写入。
# coding:utf-8
# 写入字节数据
with open("write_example1.txt", "wb") as file:
content = "Hello, World!\n"
file.write(content.encode("utf-8"))