10-作业(文件读写打包模块)

2018-07-26  本文已影响0人  李小萌mmm

files.py

# !/usr/bin/env/python
# .*. encoding:utf-8 -*-
import json

#文本文件读取
def read_file(path):
    with open(path,'r',encoding='utf-8') as f:
        content = f.read()
    print(content)
    return content


#文本文件写入
def write_file(path,content):
    with open(path,'w',encoding='utf-8') as f:
        f.write(content)


#二进制文件读取
def read_file_b(path):
    with open(path,'rb') as f:
        content = f.read()
    print(content)
    return content

#二进制文件写入
def write_file_b(path,new_path):
    with open(path,'rb') as f:
        content =f.read()

    with open(new_path, 'wb') as f:
        f.write(content)



#json文件的读取
def read_json(path):
    with open(path, 'r', encoding='utf-8') as f:
        content = json.load(f)
    print(content)
    return content


#json文件的写入
def write_json(path,content):
    with open(path, 'w', encoding='utf-8') as f:
        json.dump(content,f)




if __name__ == '__main__':
    pass

test.py

import files
content1  = files.read_file('a.txt')

content2 = files.write_file('aabbbbwww.txt',content='xiaomeng')

content3 = files.read_file_b('00002222.jpg')

content4 = files.write_file_b('00002222.jpg','11100.jpg')


content5 =files.read_json('new.json')

content6 =files.write_json('hhh.json',content='abcdefg')
上一篇 下一篇

猜你喜欢

热点阅读