zoukankan      html  css  js  c++  java
  • 文件操作

    一、打开文件

    以什么编码方式储存的文件,就以什么编码方式打开进行操作。
    格式:文件句柄 = open("文件路径", mode="r", encoding="utf-8")    #默认打开模式是"r", 可不写 mode="r"

    二、操作文件

    # 操作方式有:r  w  a  rb  wb  ab  r+  w+  a+  r+b  w+b  a+b
        r    只读模式           默认模式,文件必须存在
        w    只写模式           不可读;不存在则创建;存在则清空内容
        a    只追加写模式        不可读;不存在则创建;存在则只追加内容
    # 对于视频、图片等非文本文件,采用  rb  wb  ab  操作
        r+    读写       可读可写
        w+    写读       可写可读
        a+    写读       可写可读
    # 以bytes类型操作
        r+b   读写       可读可写
        w+b   写读       可写可读
        a+b   写读       可写可读

    只读:

    #假如有个txt格式文件,里面写着:你好吗
    
    #以str方式读出来
    f = open('text1', encoding='utf-8')    #默认mode="r",可不写
    content = f.read()
    print(content)    #你好吗
    f.close()
    
    #以bytes类型读出来
    f = open('text1', mode='rb')
    content = f.read()
    print(content)    #b'xe4xbdxa0xe5xa5xbdxe5x90x97'
    f.close()

    只写:

    #没有此文件就会创建文件
    f = open('text2', mode='w', encoding='utf-8')
    f.write('我真帅')
    f.close()
    
    # 有此文件,先将源文件的内容全部清除,在写
    f = open('text2', mode='w', encoding='utf-8')
    f.write('还是很帅')
    f.close()

    bytes写读:

    #
    f = open('text1', mode='wb')
    f.write('你好'.encode('utf-8'))
    f.close()
    
    #
    f = open('text1', mode='rb')
    content = f.read()
    print(content)
    f.close()

    读写:一般用 r+ 就够了

    f = open('text1', mode='r+', encoding='utf-8')
    print(f.read())    #在干嘛
    f.close
    
    f.write('在想你')
    f.close
    
    print(f.read())    #在想你
    f.close

    seek:指定光标移动到某个位置

    #假如文件里写着:爸爸妈妈去哪里了
    
    f = open('text1', mode='r+', encoding='utf-8')
    f.seek(3)    #seek是按照字节定光标的位置('utf-8'一个中文用3个字节表示)
    print(f.read())    #爸妈妈去哪里了
    f.close()

    read(n) :n为数字,读多少

    #假如文件里写着:爸爸妈妈去哪里了
    f = open('text1', mode='r+', encoding='utf-8')
    print(f.read(3))    #爸爸妈
    f.close()

    tell() :获取光标当前的位置

    f = open('text1', mode='r+', encoding='utf-8')
    f.write('I heard the echo, from the valleys and the heart
    Open to the lonely soul of sickle harvesting')
    print(f.tell())    #94
    f.close()

    truncate:截取文件

    f = open('text1', mode='r+', encoding='utf-8')
    f.write('From the distance, it looked like a skinny tube, 
    '
            'but as we got closer, we could see it flesh out 
    '
            'before our eyes. It was tubular, all right, but 
    '
            'fatter than we could see from far away')
    f.seek(0)
    print("first read:
    ", f.read())
    
    f.seek(0)
    f.write('my name is ppd')
    f.truncate()
    f.seek(0)
    print("second read:
    ", f.read())
    
    
    first read:
     From the distance, it looked like a skinny tube, 
    but as we got closer, we could see it flesh out 
    before our eyes. It was tubular, all right, but 
    fatter than we could see from far away
    second read:
     my name is ppd

    但凡以后写代码遇到修改文件都是读一个,改一个,删除文件,重命名文件

    具体步骤如下:

    # 假设源文件写着:
                   #name:ppd
                   #animal:老虎
    with open('text', encoding='utf-8') as f1, open('text.bak', 'w', encoding='utf-8') as f2:
        for line in f1:
            if '老虎' in line:
                line = line.replace('老虎', '狮子')
            f2.write(line)

    接着执行以下程序就达成修改文件了

    import os
    os.remove('text')  #删除原文件
    os.rename('text.bak', 'test')  #重命名文件(取代原文件、即是修改文件了)

    其他操作

    readlines():每一行当成列表中的一个元素,添加到list中

    # name:ppd
    # animal:狮子
    
    f = open('test', mode="r", encoding='utf-8')
    line = f.readlines()
    print(line)     #['name:ppd
    ', 'animal:狮子']

    readline()  一行一行读

    # name:ppd
    # animal:狮子
    
    f = open('test', mode="r", encoding='utf-8')
    line = f.readline()
    print(line)     #name:ppd
  • 相关阅读:
    嵌入式:指针的指针、链表、UCOS 的 OSMemCreate 。
    嵌入式&iOS:回调函数(C)与block(OC)传 参/函数 对比
    嵌入式&iOS:回调函数(C)与block(OC)回调对比
    iOS:以前笔记,未整理版。太多了,先放着吧。。。。。。。
    嵌入式:小技巧(慢慢回忆更新)(16.12.17更)
    嵌入式:J-link刷固件(坑)
    iOS:GCD组
    Android Studio教程07-Fragment的使用
    Android Studio教程06-布局,监听器以及基本控件
    Android Studio教程05-Parcelables和Bundles.md
  • 原文地址:https://www.cnblogs.com/believepd/p/9550979.html
Copyright © 2011-2022 走看看