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

    #读模式r 只要沾上了r文件不存在会报错
    #写模式w 只要沾上了w都会清空原来的内容
    #追加模式a
    #读模式r 读写模式 r+ 1、不能写 2、文件不存在会报错file=open('words','r',encoding='utf-8')
    #写模式w 写读模式w+ 1、打开已存在的文件写入,会把原来的内容覆盖 2、 打开不存在的文件写入,会新建一个文件 3、不能读 file=open('words','w',encoding='utf-8')
    #追加模式a 追加读模式a+ 1、打开已存在的文件写入,文件末尾追加内容 2、 打开不存在的文件写入,会新建一个文件 3、不能读 file=open('words3','a',encoding='utf-8')

    #file=open('words','w',encoding='utf-8')#w打开已存在的文件写入,会把原来的内容覆盖
    #file=open('words2','w',encoding='utf-8')#打开不存在的文件写入,会新建一个文件
    #print(file.read())#读文件
    #print(file.readline())#读取一行内容
    #print(file.readlines())#把文件的每一行放到list里面
    #file.write("新内容")

    file=open('words2','a+',encoding='utf-8')
    file.write("=====追加新内容")
    print(file.read())

    #a模式默认文件指针在末尾
    file.seek(0)#移动文件指针到最前面
    print('read----',file.read())
    file.write(' joojj')#移动完文件指着之后,是只能读,写的时候还是在末尾写
    #file.seek(0)#移动文件指针到最前面
    #print("readline===========",file.readline())
    #文件指针


    with open('','a+') as f:  #自动关闭文件
    for line in f :
    pass
  • 相关阅读:
    跨域访问方法列举 jsonp 和 客户端
    session 垃圾回收机制
    php 根据数据权重,得分或者持有数量等进行均衡分配给定数量分配方法
    进程和线程比较
    redis 过期策略分析
    redis 基础知识详解
    tcp/ip 协议
    ip 协议详解
    php redis 分布式类
    nginx打开目录游览功能
  • 原文地址:https://www.cnblogs.com/irisx/p/8688316.html
Copyright © 2011-2022 走看看