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

    w 只能操作写入 r 只能读取 a向文件追加
    wb 只能操作写入进制数据 rb 只能读取进制数据 ab向文件追加进制数据
    w+ (先清除在写)可写可读 r+(先读再写)可读可写 a+ 可读可写
    wb+ 可写入进制数据
    如果用r模式 文件必须存在,不然报错
    w模式打开文件,如果文件中有数据,再次写入内容,会把原来的覆盖掉
    文件不能修改

     1 #打开文件,打开方式utf-8(没有赋变量,无法后续操作)
     2 data=open("yesterday",encoding="utf-8").read()#相对路径打开
     3 
     4 #只读
     5 f=open("yesterday",'r',encoding="utf-8")#bytes-->str
     6 可读取非文字文件
     7 f = open("yesterday",'rb')#即 binary mode,read()操作返回的是bytes
     8 
     9 # 只写
    10 #对于w:没有此文件就会创建文件
    11 # f = open('log',mode='w',encoding='utf-8')
    12 # f.write('骑兵步兵')
    13 # f.close()
    14 # 先将源文件的内容全部清除,在写。
    15 # f = open('log',mode='w',encoding='utf-8')
    16 # f.write('附近看到类似纠纷')
    17 # f.close()
    18 # f = open('log', mode='wb')
    19 # f.write('附近看到类似纠纷'.encode('utf-8'))#用utf-8编码方式写入
    20 # f.close()
    21 # (先清除在写)可写可读
    22 # f = open('log',mode='w+',encoding='utf-8')
    23 # f.write('aaa')
    24 # f.seek(0)
    25 # print(f.read())
    26 # f.close()
    27 
    28 #只追加
    29 # f = open('log',mode='a',encoding='utf-8')
    30 # f.write('佳琪')
    31 # f.close()
    32 # f = open('log',mode='ab')
    33 # f.write('佳琪'.encode('utf-8'))
    34 # f.close()
    35 
    36 # 可读可写
    37 # f = open('log',mode='a+',encoding='utf-8')
    38 # f.write('佳琪')
    39 # f.seek(0)#重置光标位置
    40 # print(f.read())
    41 # f.close()
    42 
    43 # 读写
    44 # f = open('log',mode='r+',encoding='utf-8')
    45 # print(f.read())
    46 # f.close()
    47 
    48 
    49 
    50 #文件句柄
    51 f=open("yesterday2",'w',encoding="utf-8")
    52 #data=f.read()#读取(光标移到了最后一行,无法再读取)
    53 f.write("哈哈真好玩,
    ")#写入
    54 f.write("那必须的。")
    55 f.close()
    56 """
    57 #for i in range(5):
    58 #    print(f.readline())#读取一行
    59 #for line in f.readlines():#f.readlines结果是一个列表
    60 #    print(line)

    循环

     1 #读取整个文件后循环(占内存)
     2 for index,line in enumerate(f.readlines())
     3     if index==9:
     4         print("---分割---")
     5         continue
     6     print(line)
     7 """
     8 """
     9 #只循环当前一行
    10 count=0
    11 for index in f:
    12     if count==9:
    13         print("------分割线-------")
    14         count += 1
    15         continue
    16     print(index.strip())
    17     count+=1
    18 
    19 with open("yesterday2",'w',encoding="utf-8") as ff:#和open()一样,但无需close()
    20 #with open("yesterday2",'r',encoding="utf-8") as ff,
    21     open("yesterday",'a',encoding="utf-8") as fff:
    22     print(ff.read())

    函数

    f = open('log',mode='a',encoding='utf-8')#log文件名
    # print(f.tell())#读取光标位置(按字节找)
    # print(f.read(30))#读字符
    # print(f.tell())
    # f.seek(0)#重置光标位置(按字节找)
    # print(f.tell())
    # f.readable()#判断是否可读
    # f.readline()#一行一行的读
    # f.readlines()#每一行当成列表中的元素,添加到列表中
    # f.truncate(5)#对原文件进行截取,其余删除

     文件“修改”

    1 #文件假修改(原文件->str(修改)->新文件->原文件删除)
    2 with open('log',encoding='utf-8') as f,open('log.bak','w',encoding='utf-8') as f2:
    3     for line in f:
    4         if "佳琪" in line:
    5             line=line.replace('佳琪','琪琪')
    6             f2.write(line)
    7 import os
    8 os.remove('log')#删除
    9 os.rename('log.bak','log')#重命名
    原作者博客链接:https://www.cnblogs.com/cgqForward
  • 相关阅读:
    读书笔记之理想设计的特征
    一些javascript 变量声明的 疑惑
    LINQ 使用方法
    Google MySQL tool releases
    读书笔记之设计的层次
    EF之数据库连接问题The specified named connection is either not found in the configuration, not intended to be used with the Ent
    转载 什么是闭包
    javascript面向对象起步
    Tips
    数据结构在游戏中的应用
  • 原文地址:https://www.cnblogs.com/cgqForward/p/9572215.html
Copyright © 2011-2022 走看看