• Python文件操作


    -*- coding:utf-8 -*-
    f = open('t2',mode='r',encoding='utf-8')
    打开 第一个内容是文件的名字(必须是字符串)
    mode 第二个内容是咱们对这个文件的操作方式 只读
    encoding 第三个内容是咱们这个文件的编码集
    f 文件句柄所有对文件的操作都是操作文件句柄   锅把
    r模式 读取字符

    content = f.read()
    读 一次性全部读取
    content1 = f.readline()
    # 读取一行,但是有个 换行
    print(content1)
    content2 = f.readline()
    print(content2)

    content = f.readlines()
    readlines 读取一行一行的,存放在列表里
    print(content)

    content = f.read()
    # mode = 'r' read里的内容就是表示读取的字符数量
    print(content)

    f = open('C:\Users\oldboy\Desktop\QQ.jpg',mode='rb')
    f = open(r'C:UsersoldboyDesktopQQ.jpg',mode='rb')
    r 和 \ 一样都是转义
    rb读取字节

    从磁盘开始查找的就是绝对路径

    C:UsersoldboyDesktopQQ.jpg
    print(f.read())


    f = open('QQ.jpg',mode='rb')
    # 相对路劲 相对于 04 内容详解来查找的一种路径
    print(f.read())

    推荐写法 相对路径

    f = open('t2',mode='r',encoding='utf-8')
    print(f.read())


    w 模式
    f = open('t2.txt',mode='w',encoding='utf-8')
    f.write('在网上也是这样。')
    f.write('人生三个爽')
    w模式 如果文件不存在就新建一个
    覆盖写,写之前先把文件清空

    wb 模式
    f = open('QQ.jpg',mode='rb')

    f1 = open('qq2.jpg',mode='wb')
    f1.write(f.read())

    a 模式 追加

    f = open('t2',mode='a',encoding='utf-8')
    f.write('相对路径')
    文件最后添加内容

     1.w只写 没有读的功能
    f = open('t2',mode='w',encoding='utf-8')


    f = open('1.doc',mode='rb')
    print(f.read())


    可读可写

    r+ #读 写
    w+ #写 读
    a+ #追加 读

    f = open('t2',mode='r+',encoding='utf-8')
    print(f.read()) # 读内容
    f.write("今天是周一,你们还棒啊")

    f.write('"今天是周一,你们挺棒啊"')
    print(f.read()) # 错误的示范

    f = open('t2',mode='w+',encoding='utf-8')
    f.write('老子今天上班了')
    f.seek(0) #头 # 移动文件光标
    print(f.read())

    f = open('t2',mode='a+',encoding='utf-8')
    f.seek(0)
    print(f.read())
    f.write('哈哈哈')

    最常用的r+


    f = open(r'C:UsersoldboyPycharmProjectss19day6 2.txt',mode='r+',encoding='utf-8')
    print(f.read()) # 读内容
    f.write("今天是周一,你们还棒啊")

    f = open(r'..day6 2.txt',mode='r+',encoding='utf-8')
    # .. 返回上一级
    print(f.read()) # 读内容
    f.write("今天是周一,你们还棒啊")


    f = open(r'..day6 2.txt',mode='r',encoding='utf-8')
    print(f.read())

    总结:
    r 只读 不能写
    read() 读取全部
    模式是r的时候 read(3) 就是读取3个字符
    模式是rb的时候 read(3) 就是读取3个字节

    readline() 读取一行
    readlines() 存放在一个列表中

    w 只写 不能读
    覆盖写,先清空文件的内容,在写
    当文件不存在的时候,创建一个文件

    a 追加写 不能读
     写的内容一直都在文件的末尾

    r+ 读写
    错误示范 先写后读
    正确的是 先读后写

    w+ 写读
    写读的时候是读不到内容的,除非移动光标

    a+ 追加写 读
    写读的时候是读不到内容的,除非移动光标

    最常用: r,w,a,r+,a+

    f = open('t2',mode='r',encoding='utf-8')
    # print(f.read())
    msg = f.read()
    print(msg)
    f.close()


    f = open('t2',mode='w',encoding='utf-8')
    # print(f.read())
    msg = f.write('有问题就要问呢,哥哥们') # 向文件里写了多少个字符
    f.flush() # 刷新
    print(msg)
    f.close()

    with open('t2',mode='w',encoding='utf-8')as f:#上下文管理 --- 面试题
    msg = f.write('小强和刘申')
    print(msg)
    不用你自动关闭文件

    其他操作:

    with open('t2','rb')as f:
    print(f.read())
    print(f.seek(0)) # 文件头部
    print(f.read(1))
    print(f.tell()) # 查看光标的位置 数的是字节


    with open('t2','r',encoding='utf-8')as f:
    print(f.read())
    f.seek(2) # 字节
    f.seek(0,1)
    双个数字
    0,0 文件头部
    0,1 当前位置
    0,2 文件尾部

    单个数字
    f.seek(6) # 字节

    print(f.read())
    print(f.read(1))
    print(f.tell()) # 查看光标的位置 数的是字节

    with open('t2','r+',encoding='utf-8')as f:
    f.truncate(9) # 3个字符 指定字节之后的门内容全部删除

    f = open('t2',mode='r+',encoding='utf-8')
    print(f.read(4))
    print(f.tell())
    f.truncate()

    import os
    os.rename('t2','t3')

    with open('t3','r',encoding='utf-8')as f,
    open('t5','a',encoding='utf-8')as f1:
    msg = f.read() # 将源文件复制一份
    msg = msg.replace('好','困')
    f1.write(msg)

    import os
    os.remove('t3') # 删除原数据
    os.rename('t5','t3')


    with open('t3',mode='r',encoding='utf-8')as f,
    open('t5',mode='a',encoding='utf-8')as f1:
    for i in f:
    i = i.strip().replace('父','爸爸')
    f1.write(i)
    print(f.readline().strip())

    with open('t3',mode='r+',encoding='utf-8')as f:
    f.read(4) #
    print(f.tell()) # 字节 4 * 3 == 12
    f.truncate(12)

  • 相关阅读:
    spring 条件装配
    git 工作用命令
    tomcat 在 server.xml 中配置数据源
    rancher + harbor + jenkins 自动化部署
    FHS 文件体系标准
    linux常用命令
    linux常用工具
    ts-node 执行报错,Cannot find module '@types/node/package.json'
    npm 安装报错 gyp: No Xcode or CLT version detected!
    css中的层级问题(z-index)
  • 原文地址:https://www.cnblogs.com/YZL2333/p/10243933.html
Copyright © 2020-2023  润新知