模块名字homework
import json
# 1.对文本文件的读操作
def func1(address):
"""address是读操作需要的路径"""
f1 = open(address, 'r', encoding='utf-8')
# 进读取文本文件
content = f1.read()
print(content)
# 3.关闭文件
f1.close()
#2.对文本文档的写操作
def func2(address,message):
f2 = open(address, 'w', encoding='utf-8')
# print(type(fo1))
f2.write(message)
# 3.关闭文件
f2.close()
#3.对二进制文件的读操作
def func3(address):
f3 = open(address,'rb')
image = f3.read()
print(image)
#关闭文件
f3.close()
#4.对文件的写操作
def func4(address,message):
f4 = open(address,'wb')
f4.write(message)
#关闭文件
f4.close()
#5.对json文件的读操作
def func5(address):
with open(address, 'r', encoding='utf-8') as f:
content = f.read()
print(content)
#对json文件的写操作
def func6(address,message):
with open(address, 'w', encoding='utf-8') as f6:
w_content = message
json.dump(w_content, f6)
调用模块
from homework import func1
func1('./test.txt')
网友评论