函数的简单封装
实现对文件读写操作的封装
- file_function.py(实现函数的封装)
"""文本文件的读操作"""
def file_r(url):
with open(url,'r',encoding='utf-8') as f_r:
file = f_r.read()
return file
"""二进制文件的读操作"""
def file_rb(url):
with open(url, 'rb') as f_r:
file = f_r.read()
return file
"""文本文件的写操作"""
def file_w(url, str1):
with open(url, 'w', encoding='utf-8') as f_r:
f_r.write(str1)
return
"""二进制文件的写操作"""
def file_wb(url, str1):
with open(url, 'wb') as f_r:
f_r.write(str1)
return
if __name__ == '__main__':
pass
- (进行函数调用)
import file_function
if __name__ == '__main__':
print(file_function.file_r('./test.txt'))
print(file_function.file_rb('./1.jpg'))
file_function.file_w('./king.txt','KingJX')
网友评论