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
  • 相关阅读:
    [Leetcode] Rotate Image
    [Leetcode] Permutation Sequence
    [Leetcode] Palindrome Partitioning
    [Leetcode] Letter Combinations of a Phone Number
    Java里的多线程
    css学习2----css动态菜单
    css学习1----css超链接效果
    javascript判断身份证是否合法
    RMI(Remote Method Invocation,远程方法调用)
    struts.properties配置详解
  • 原文地址:https://www.cnblogs.com/irisx/p/8688316.html
Copyright © 2011-2022 走看看