zoukankan      html  css  js  c++  java
  • day-2 python 文件读写模式r,r+,w,w+,a,a+的区别

    r+和w+都是可读可写,区别在于r+读文件时,不会覆盖之前的内容,之前的内容能够读出来;w+读时,会覆盖之前的内容;所以读文件时,用r或者r+

    #读操作 r

    1 filepath = 'aa.log' #aa.log存在
    2 #读操作
    3 #read()方法,一次都读出来
    4 with open(filepath,'r') as f:
    5     print(f.read())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    hello world!
    
    
    Process finished with exit code 0
    1 filepath = 'a.log'  #a.log不存在,执行报错
    2 #读操作
    3 #read()方法,一次都读出来
    4 with open(filepath,'r') as f:
    5     print(f.read())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    Traceback (most recent call last):
      File "E:/python/文件读写.py", line 4, in <module>
        with open(filepath,'r') as f:
    FileNotFoundError: [Errno 2] No such file or directory: 'a.log'
    
    Process finished with exit code 1

    #读写操作 r+

    1 #r+写操作
    2 filepath = 'aa.log'
    3 with open(filepath, 'r+') as f:
    4     f.write('文字被替换成hahaha')
    5     print(f.read())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    被替换成hahaha
    
    Process finished with exit code 0

    #readline()读操作

    1 filepath = 'my.log'
    2 with open(filepath,'r',encoding='UTF-8') as f:
    3     print(f.readline().strip())
    4     print(f.readline().strip())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    2019-10-12 09:34:23,073 - E:/python/test.py[line:9] - DEBUG: 默认日志级别是debug
    2019-10-12 09:34:23,075 - E:/python/test.py[line:10] - INFO: info级别
    
    Process finished with exit code 0

    #readlines()读操作,读到的内容,返回一个列表

    1 #readlines()方法,一次都读出来,并返回一个列表
    2 with open(filepath,'r',encoding='UTF-8') as f:
    3     print(f.readlines())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    ['2019-10-12 默认日志级别是debug
    ', '2019-10-12 info级别
    ', '
    ']
    
    Process finished with exit code 0
    1 #将readlines读到的内容存放到line中,使用for循环遍历内容
    2 with open(filepath,'r',encoding='UTF-8') as f:
    3     lines = f.readlines()
    4 for line in lines:
    5     print(line.strip())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    2019-10-12 默认日志级别是debug
    2019-10-12 info级别
    
    
    Process finished with exit code 0
    1 # 如果要逐行读取,直接遍历文件对象就可以了
    2 with open(filepath,'r',encoding='UTF-8') as f:
    3     for line in f:
    4         print(line.strip())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    2019-10-12 默认日志级别是debug
    2019-10-12 info级别
    
    
    Process finished with exit code 0
    1 #写操作
    2 with open(filepath,'w',encoding='UTF-8') as f:
    3     f.write('hello world!
    ')
    4 
    5 with open(filepath,'r',encoding='UTF-8') as f:
    6     print(f.read())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    hello world!
    
    
    Process finished with exit code 0

    #w+读写模式

    1 with open(filepath,'w+',encoding='UTF-8') as f:
    2     f.write('hello world!
    ')
    3     f.seek(0)
    4     print(f.read())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    hello world!
    
    
    Process finished with exit code 0

    #a+追加读写

    1 with open(filepath,'a+',encoding='UTF-8') as f:
    2     f.write('hello world!
    ')
    3     f.seek(0)
    4     print(f.read())
    C:UsersadminMiniconda3python.exe E:/python/文件读写.py
    hello world!
    hello world!
    
    
    Process finished with exit code 0
     1 #读模式,只能读,不能写,默认读模式
     2 f = open(r'E:cnzday1列表.py','r',encoding='utf-8')
     3 result = f.read()
     4 print(result)
     5 f.close()
     6 
     7 #读模式r 写模式w 追加模式a
     8 #写模式,只能写,不能读,写模式会覆盖之前的内容
     9 
    10 f = open('test.txt','w',encoding='utf-8')
    11 f.write('abc')
    12 f.close()
    13 
    14 # 追加模式,写文件时,不会覆盖之前的内容
    15 f = open('test.txt','a',encoding='utf-8')
    16 # f.write('哈哈哈')
    17 f.read()
    18 f.close()
    19 
    20 # 总结:
    21 # 读模式,只能读,不能写,打开不存在的文件会报错
    22 # 写模式,只能写,不能读,打开不存在的文件会创建
    23 # 追加模式,只能写,不能读,在原文件基础上增加内容,打开不存在的文件,会创建
    24 # 只要跟r有关的,打开不存在的文件,会报错
    25 # 只要和w有关,都会清空之前的文件
    26 # a+模式,文件指针模式是在末尾的,如果想读文件,需要seek(0)
    27 #writelines传一个list,会把list里面的元素写到文件里
    28 
    29 # r+       w+       a+
    30 # 读写模式  写读模式  追加读模式
    31 
    32 f = open('test3.txt','a+',encoding='utf-8')
    33 f.write('哈哈哈1')
    34 f.read()
    35 f.close()
    36 
    37 l = ['a','b','c','d']
    38 f = open('test.txt','w',encoding='utf-8')
    39 f.writelines(l)
    40 f.close()
  • 相关阅读:
    JavaWeb--HttpSession案例
    codeforces B. Balls Game 解题报告
    hdu 1711 Number Sequence 解题报告
    codeforces B. Online Meeting 解题报告
    ZOJ 3706 Break Standard Weight 解题报告
    codeforces C. Magic Formulas 解题报告
    codeforces B. Sereja and Mirroring 解题报告
    zoj 1109 Language of FatMouse 解题报告
    hdu 1361.Parencodings 解题报告
    hdu 1004 Let the Balloon Rise 解题报告
  • 原文地址:https://www.cnblogs.com/hujc/p/11675833.html
Copyright © 2011-2022 走看看