函數(shù)的簡單封裝
實(shí)現(xiàn)對(duì)文件讀寫操作的封裝
- file_function.py(實(shí)現(xiàn)函數(shù)的封裝)
"""文本文件的讀操作"""
def file_r(url):
with open(url,'r',encoding='utf-8') as f_r:
file = f_r.read()
return file
"""二進(jìn)制文件的讀操作"""
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
"""二進(jìn)制文件的寫操作"""
def file_wb(url, str1):
with open(url, 'wb') as f_r:
f_r.write(str1)
return
if __name__ == '__main__':
pass
- (進(jìn)行函數(shù)調(diào)用)
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')