DAY9-作业
2018-07-27 本文已影响0人
陈卷卷卷卷
封装一个模块,可以在调用这个模块时能够读写指定的文件。
"""---chen---"""
import json
# 声明txt文件读取函数
def file_read(address):
try:
with open(address, 'r', encoding='utf-8') as f:
content = f.read()
return content
except FileNotFoundError:
print('没有找到这个文件')
return 0
# 声明txt文件写入函数
def file_write(address):
with open(address, 'w', encoding='utf-8') as f:
content = input('您行输入什么到文件中:')
f.write(content)
return f.write(content)
# 声明二进制文件读取函数
def bin_file_read(address):
try:
with open(address, 'rb') as f:
f.read()
return f.read()
except FileNotFoundError:
print('文件没有找到')
return 0
# 声明二进制文件写入函数
def bin_file_write(address):
try:
with open(address, 'bw') as f:
content = input('您行输入什么到文件中:')
f.write(content)
return f.write(content)
except TypeError:
print("输入数据类型错误")
# 声明json文件读取函数
def json_file_read(address):
try:
with open(address, 'r', encoding='utf-8') as f:
content = json.load(f)
print('%c文件的内容为:' % address, content)
except FileNotFoundError:
print('文件不存在')
return 0
# 声明json文件读写操作
def json_file_write(address):
with open(address, 'w', encoding='utf-8') as f:
content = input('输入您要保存的文件:')
json.dump(content, f)
if __name__ == "__main__":
pass