zoukankan      html  css  js  c++  java
  • 04-Python基础之文件操作基础

    Python基础之文件操作

    1、文件的打开方式

    读文件的方式有四种:

      r:只读

      rb:二进制读取

      r+:读写,先读在写,在文件光标处接着写

      r+b:二进制读取数据,英文读取英文,中文转为二进制

    # #文件操作,打开一个文件,读取文件内容,返回文件内容为字符串
    f = open('info', mode='r', encoding='utf-8')
    content = f.read()
    print(content, type(content))
    f.close()
    # #二进制读取
    f = open('info', mode='rb')
    content = f.read()
    print(content, type(content))
    f.close()
    
    #读写,先读出内容,再在结尾处写文件
    f = open('info',mode='r+',encoding='utf-8')
    print(f.read())
    f.write('
    大猛,小孟')
    f.close()
    
    #中文转为二进制的数据,英文字母不转,返回
    f = open('info', mode='r+b')
    print(f.read())
    f.write('大猛,小孟'.encode('utf-8'))
    f.close()

    写文件的方式也有四种:

    w ,wb:区别不大

    w+ :写完之后重置光标才能读取,否则光标在最后,读取不到内容

    w+b:写的内容需要

    f = open('info',mode='w',encoding='utf-8')   
    f.write('附近看到类似纠纷')                          
    f.close()                                    
    #wb                                          
    f = open('log',mode='wb')                    
    f.write('附近看到类似纠纷'.encode('utf-8'))          
    f.close()                                    
      # w+ w+b写的内容需要加二进制形式                       
    f = open('info',mode='w+b')                  
    f.write(b'aaa')    #f.write('aaa'.encode('utf8')) 
    f.seek(0) print(f.read()) f.close() 

    追加方式:

    f = open('info',mode='a',encoding='utf8')
    f.write('佳琪')
    f.close()

     注意:读出来的都是字符串即是str类型,

    2.文件的其他操作

    #read,readline,readlines区别
    #一次性读完,之后for循环相当于对所有内容(内容为str)循环遍历
    f = open('info',mode='r',encoding='utf8') #['a', 'a', 'a', '佳', '琪', '佳', '琪', '', '1', '2', '3', '3', '3', '3', '3', '3', '3', '3', '', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2']
    read_L= []
    for line in f.read():
        read_L.append(line.strip())
    f.close()
    print(len(read_L))
    print(read_L)
    
    #只读一行,之后for循环相当于对所有内容(内容为str)循环遍历
    f = open('info',mode='r',encoding='utf8')   #['a', 'a', 'a', '佳', '琪', '佳', '琪', '']
    readline_L= []
    for line in f.readline():
        readline_L.append(line.strip())
    f.close()
    print(len(readline_L))
    print(readline_L)
    
    #每行读完当一个列表
    f = open('info',mode='r',encoding='utf8') #['aaa佳琪佳琪', '1233333333', '1111111111', '2222222222']
    readlines_L= []
    for line in f.readlines():
        readlines_L.append(line.strip())
    f.close()
    print(len(readlines_L))
    print(readlines_L)

    修改文件

    with open('info',encoding='utf-8') as f,open('info.bak','w',encoding='utf-8') as f2:
        for line in f:
            if 'aaa' in line:  #班主任:星儿
                line = line.replace('aaa','******')
            #写文件
            f2.write(line) #小护士:金老板
    
    import os
    os.remove('info') #删除文件
    os.rename('info.bak','info')  #重命名文件

     注意:文件使用过程中要习惯使用with open()打开文件,这样会自动关闭文件,而不用输入f.close(),且只在Python3才有with open()操作,py2不支持这个操作

    自学课程参照老男孩全栈视频
  • 相关阅读:
    Redis5设计与源码分析 (第17章 HyperLogLog相关命令的实现)
    Redis5设计与源码分析 (第16章 GEO相关命令)
    ES5和ES6函数的this指向
    vue响应式原理 (响应式并不等于数据双向绑定,千万不要混淆)
    vue中data为什么是函数而不是对象
    vue-enum 前端常量 枚举工具
    Vue3 写业务逻辑不适合用TS(TypeScript)
    vue-property-decorator vue3 ts 用的装饰器
    github git clone下载加速 && npm install 下载加速
    vue3 如果用ts,导出时候要用 defineComponent,这俩是配对的,为了类型的审查正确
  • 原文地址:https://www.cnblogs.com/chenrongjin/p/10014534.html
Copyright © 2011-2022 走看看