• day8 文件操作


    day8 文件操作
    1,文件操作。
    模特主妇护士老师.txt
    1,文件路径:d:模特主妇护士老师.txt
    2,编码方式:utf-8 gbk 。。。。
    3,操作方式:只读,只写,追加,读写,写读.....
    以什么编码方式储存的文件,就以什么编码打开进行操作。


    只读:r
    rb
    f = open('模特主妇护士班主任',mode='r',encoding='utf-8')
    content = f.read()
    print(content,type(content))
    f.close()

    # f = open('模特主妇护士班主任',mode='rb',)
    # content = f.read()
    # print(content)
    # f.close()

    r+ 读写
    r+b 读写(以bytes类型)
    # f = open('log',mode='r+',encoding='utf-8')
    # print(f.read())
    # f.write('大猛,小孟')
    # f.close()

    f = open('log',mode='r+b')
    print(f.read())
    f.write('大猛,小孟'.encode('utf-8'))
    f.close()

    只写:w
    wb
    # 先将源文件的内容全部清除,在写。
    # f = open('log',mode='w',encoding='utf-8')
    # f.write('附近看到类似纠纷')
    # f.close()

    f = open('log',mode='wb')
    f.write('附近看到类似纠纷'.encode('utf-8'))
    f.close()
    w+
    # f = open('log',mode='w+',encoding='utf-8')
    # f.write('aaa')
    # f.seek(0)
    # print(f.read())
    # f.close()
    w+b
    .......


    追加
    # f = open('log',mode='a',encoding='utf-8')
    # f.write('佳琪')
    # f.close()

    # f = open('log',mode='ab')
    # f.write('佳琪'.encode('utf-8'))
    # f.close()


    2,编码。

    3,试卷讲解。
    绝对路径
    f = open('d:模特主妇护士班主任.txt',mode='r',encoding='UTF-8')
    content = f.read()
    print(content)
    f.close()

    #bytes ---->str
    f = open('模特主妇护士班主任',mode='r',encoding='utf-8')
    content = f.read()
    f.write('fjsdlk')
    f.close()

    f = open('模特主妇护士班主任',mode='rb',)
    content = f.read()
    print(content)
    f.close()
    f = open('log',mode='r+',encoding='utf-8')
    print(f.read())
    f.close()

    f = open('log',mode='r+b')
    print(f.read())
    f.write('大猛,小孟'.encode('utf-8'))
    f.close()

    #对于w:没有此文件就会创建文件
    f = open('log',mode='w',encoding='utf-8')
    f.write('骑兵步兵')
    f.close()

    先将源文件的内容全部清除,在写。
    f = open('log',mode='w',encoding='utf-8')
    f.write('附近看到类似纠纷')
    f.close()


    f = open('log',mode='w+',encoding='utf-8')
    f.write('aaa')
    f.seek(0)
    print(f.read())
    f.close()


    f = open('log',mode='wb')
    f.write('附近看到类似纠纷'.encode('utf-8'))
    f.close()

    f = open('log',mode='a',encoding='utf-8')
    f.write('佳琪')
    f.close()
    #
    f = open('log',mode='a',encoding='utf-8')
    f.write('佳琪')
    f.close()

    f = open('log',mode='a+',encoding='utf-8')
    f.write('佳琪')
    f.seek(0)
    print(f.read())
    f.close()


    f = open('log',mode='ab')
    f.write('佳琪'.encode('utf-8'))
    f.close()

    #以bytes类型写
    f = open("123","wb")
    f.write("山东高速的感受到".encode("gbk"))
    f.close()
    #写
    f = open("123","w",encoding="utf-8")
    f.write("sdfhdfhdfdv")
    f.close()

    #追加写
    f = open("123","a",encoding="utf-8")
    f.write(" 是德国帅哥是谁的复合方式的核实对方")
    f.close()

    #以bytes类型追加
    f = open("123","ab")
    f.write(" 哈哈哈".encode("utf-8"))
    f.close()

    #功能详解

    obj = open('log',mode='r+',encoding='utf-8')
    content = f.read(3) 读出来的都是字符
    f.seek(3) 是按照字节定光标的位置
    f.tell() 告诉你光标的位置
    print(f.tell())
    content = f.read()
    print(content)
    f.tell()
    f.readable() 是否刻度
    line = f.readline() 一行一行的读
    line = f.readlines() 每一行当成列表中的一个元素,添加到list中
    f.truncate(4)
    for line in f:
    print(line)
    f.close()

    f = open('log',mode='a+',encoding='utf-8')
    f.write('佳琪')
    count = f.tell()
    f.seek(count-9)
    print(f.read(2))
    f.close()

    with open('log',mode='r+',encoding='utf-8') as f,
    open('log',mode='w+',encoding='utf-8') as f1:

  • 相关阅读:
    Android应用安全之外部动态加载DEX文件风险
    Android应用安全之Android APP通用型拒绝服务漏洞
    利用Cydia Substrate进行Android HOOK(二)
    Apk去签名校验详解
    nginx部署前端项目的一些配置【刚入门】
    java 日期格式化 将String日期重新格式化成String型【转】
    idea导入maven项目时需要注意
    element 'dependency' cannot have character [children], because the type's content type is element-only.
    腾讯云centos7执行 systemctl status postfix.service 启动postfix服务时出现 /usr/sbin/postconf: fatal: parameter inet_interfaces: no local interface found for ::1
    redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
  • 原文地址:https://www.cnblogs.com/Murraya/p/10542867.html
Copyright © 2020-2023  润新知