Day9-文件操作作业

2018-07-26  本文已影响0人  晓晓的忍儿

调用模块:

from doc import general_w_doc,general_r_doc,general_a_doc,\
    general_br_doc,general_wb_doc,json_w_doc,json_dumps_doc,\
    json_loads_doc,json_r_doc

if __name__ == '__main__':
    def operation_doc(type):
        if type=='普通读':
            return general_r_doc
        if type=='普通二进制读':
            return general_br_doc
        if type=='普通写':
            return general_w_doc
        if type=='普通二进制写':
            return general_wb_doc
        if type=='普通追加':
            return general_a_doc
        if type=='json读':
            return json_r_doc
        if type=='json写':
            return json_w_doc
        if type=='json转换python':
            return json_loads_doc
        if type=='python转换json':
            return json_dumps_doc


    files1 = operation_doc('普通读')('./text1.txt')
    files2 = operation_doc('普通二进制读')('./fubo.jpg')
    operation_doc('普通写')('./text1.txt','窗前明月光')
    files3 =operation_doc('普通读')('./text1.txt')
    operation_doc('普通二进制写')('./fubo3.jpg',files2)
    files4 =operation_doc('普通二进制读')('./fubo.jpg')
    operation_doc('普通追加')('./text1.txt', '疑是地上霜')
    files5 =operation_doc('普通读')('./text1.txt')
    files6 = operation_doc('json读')('./files/text1.json')
    operation_doc('json写')('./files/text1.json',json_dumps_doc({'name': 'json', 'age': 35}))
    files7 =operation_doc('json读')('./files/text1.json')
    files8 = operation_doc('json转换python')('["a",100,false,{"a":1,"abc":100}]')
    files9= operation_doc('python转换json')({'name': 'ok', 'age': 35})
    print(files1)
    print(files2)
    print(files3)
    print(files4)
    print(files5)
    print(files6)
    print(files7)
    print(files8)
    print(files9)

功能模块:

import json
def general_r_doc(address):
    with open(address,'r',encoding='utf-8') as f:
        read_doc=f.read()
    return read_doc
def general_w_doc(address,data):
    with open(address,'w',encoding='utf-8') as f:
        write_doc=f.write(data)
    return write_doc
def general_a_doc(address,data):
    with open(address,'a',encoding='utf-8') as f:
        write_doc=f.write(data)
    return write_doc
def general_br_doc(address):
    with open(address,'br') as f:
        data_doc=f.read()
    return data_doc
def general_wb_doc(address,data):
    with open(address,'wb') as f:
        data_doc=f.write(data)
    return data_doc
def json_r_doc(address):
    try:
        with open(address,'r',encoding='utf-8') as f:
            read_r=json.load(f)
            return read_r
    except FileNotFoundError:
        open(address,'w',encoding='utf-8')
        print('不存在该json文件')
        return
def json_w_doc(address,data):
    with open(address, 'w', encoding='utf_8') as f:
        write_json = json.dump(data, f)
    return write_json

def json_loads_doc(data):
    content=json.loads(data,encoding='utf-8')
    return content
def json_dumps_doc(data):
    content=json.dumps(data)
    return content

if __name__ == '__main__':
    files=general_r_doc('./text1.txt')
    print(files)
    files1=general_w_doc('./text1.txt','窗前明月光')
    print(files1)
    files2=general_br_doc('./fubo.jpg')
    print(files2)
    files3 = general_wb_doc('./fubo2.jpg',files2)
    print(files3)
    files4 = general_a_doc('./text1.txt', '疑是地上霜')
    print(files4)
    file5 = json_r_doc('./files/text1.json')
    print(file5)
    file6=json_w_doc('./files/text1.json',json_dumps_doc({'name': 'ok', 'age': 35}))
    print(file6)
    file7=json_loads_doc('["a",100,false,{"a":1,"abc":100}]')
    print(file7)

结果:

窗前明月光疑是地上霜
{"name": "ok", "age": 35}
{"name": "json", "age": 35}
['a', 100, False, {'a': 1, 'abc': 100}]
{"name": "ok", "age": 35}

Process finished with exit code 0

上一篇 下一篇

猜你喜欢

热点阅读