美文网首页
day9作业

day9作业

作者: zzzsssr | 来源:发表于2018-07-30 15:15 被阅读42次

    模块名字homework

    import json
    # 1.对文本文件的读操作
    def func1(address):
        """address是读操作需要的路径"""
        f1 = open(address, 'r', encoding='utf-8')
        # 进读取文本文件
        content = f1.read()
        print(content)
        # 3.关闭文件
        f1.close()
    
    #2.对文本文档的写操作
    def func2(address,message):
        f2 = open(address, 'w', encoding='utf-8')
        # print(type(fo1))
        f2.write(message)
        # 3.关闭文件
        f2.close()
    
    #3.对二进制文件的读操作
    def func3(address):
        f3 = open(address,'rb')
        image = f3.read()
        print(image)
        #关闭文件
        f3.close()
    
    #4.对文件的写操作
    def func4(address,message):
        f4 = open(address,'wb')
        f4.write(message)
        #关闭文件
        f4.close()
    
    #5.对json文件的读操作
    def func5(address):
        with open(address, 'r', encoding='utf-8') as f:
            content = f.read()
            print(content)
    #对json文件的写操作
    def func6(address,message):
        with open(address, 'w', encoding='utf-8') as f6:
            w_content = message
            json.dump(w_content, f6)
    

    调用模块

    from homework  import  func1
    func1('./test.txt')
    

    相关文章

      网友评论

          本文标题:day9作业

          本文链接:https://www.haomeiwen.com/subject/trbomftx.html