zoukankan      html  css  js  c++  java
  • python 文件读取方法详解

    话不多说直接码

    # 绝对路径
    # f = open('/Users/fangxiang/Downloads/我的古诗.text', mode='r', encoding='utf-8')
    # content = f.read()
    # print(content)
    # f.close()
    
    # 相对路径
     f = open('我的古诗.text', mode='r', encoding='utf-8')
     content = f.read()
     print(content, type(content))
     print(f, type(f))
     f.close()
    
    # 1、打开非文字类文件的读取看
    # 2、上传下载储存时
     f = open('我的古诗.text', mode='rb')
     content = f.read()
     print(content)
     f.close()
    
    # 写 w
    # 对于写 没有此文件就创建文件
    # f = open('log', mode='w', encoding='utf-8')
    # f.write('高清五码')
    # f.close()
    # 先将源文件的内容全部清除,再写
    # f = open('log', mode='w', encoding='utf-8')
    # f.write('绝对好看')
    # f.close()
    
    # # 写wb 以bytes方式写入
    # f = open('log', mode='wb')
    # f.write('附件看到类型节分'.encode('utf-8'))
    # f.close()
    
    # 追加 a
    
    # f = open('log', mode='a', encoding='utf-8')
    # f.write('追加进去的内容1')
    # f.close()
    # # 追加 ab 以bytes方式追加
    # f = open('log', mode='ab')
    # f.write('追加进去的内容2'.encode('utf-8'))
    # f.close()
    
    
    # 读写 r+ 先把原文章读出来,再追加进去 也有bytes类型 r+b
    # f = open('log', mode='r+', encoding='utf-8')
    # content = f.read()
    # print(content)
    # f.write(',读后追加的内容')
    # f.close()
    # r+先写后读(光标从头开始写,再读取光标后的内容)
    # f = open('log', mode='r+', encoding='utf-8')
    # f.write('wer')
    # content = f.read()
    # print(content)
    # f.close()
    # r+b 读写 bytes形式
    # f = open('log', mode='r+b')
    # content = f.read()
    # print(content)
    # f.write(',读后追加的内容'.encode('utf-8'))
    # f.close()
    
    # 写读 w+  写后在读就读不出来了 w+b
    # f = open('log', mode='w+', encoding='utf-8')
    # f.write('先写,厚度'),
    # print(f.read())
    # f.close()
    
    # 追加 a+
    # f = open('log', mode='a+', encoding='utf-8')
    # f.write('——这里是追加的内容+')
    # f.seek(0)
    # print(f.read())
    # f.close()
    #  a+b
    
    # 功能详解
    # seek()
    # f = open('log', mode='r+', encoding='utf-8')
    # content = f.read(3)  # 读出来的都是字符 读取三个字符
    # print(content)
    
    # f.seek(3)  # 光标是按照字节去找的  是按照字节定光标
    # 断点续传
    # f.tell()  告诉你光标的位置(字节计算),然后在seek调到该位置
    
    
    # print(f.tell())
    # content = f.read()
    # print(content)
    # f.close()
    
    
    # 全查看光标,在编号光标位置,最后读取
    # f = open('log', mode='a+', encoding='utf-8')
    # f.write('——加起')
    # count = f.tell()
    # f.seek(count-9) # 读取后三中文字符
    # content = f.read()
    # print(content)
    # f.close()
    
    # f.tell() 光标位置
    # f.readable() 判断是否可读
    # f.truncate(2)  对原文件进行截取 修改源文件 截取前2个字节
    f = open('log', mode='r+', encoding='utf-8')
    # f.truncate(3)
    
    # line = f.readline()  # 一行一行的读
    # lines = f.readlines()  # 每一行当成列表中的一个元素,添加到list中
    
    # print(line)
    # print(lines)
    # for line in f:
    #     print(line)
    # f.close()
    
    
    # 打开文件方式 不需要关闭文件f.close()
    
    # 方式一
    # with open('log', mode='r+', encoding='utf-8') as obj:
    #     print(obj.read())
    
    
    # 方式二
    with open('log', mode='r+', encoding='utf-8') as f,
        open('模特主妇护士老师.text', mode='r+', encoding='utf-8') as f1:
            print(f1.read())
            print(f.read())
    

      

  • 相关阅读:
    高精度计算
    高精度除以低精度
    P1258 小车问题
    POJ 2352 stars (树状数组入门经典!!!)
    HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
    HDU 3938 Portal (离线并查集,此题思路很强!!!,得到所谓的距离很巧妙)
    POJ 1703 Find them, Catch them(确定元素归属集合的并查集)
    HDU Virtual Friends(超级经典的带权并查集)
    HDU 3047 Zjnu Stadium(带权并查集,难想到)
    HDU 3038 How Many Answers Are Wrong(带权并查集,真的很难想到是个并查集!!!)
  • 原文地址:https://www.cnblogs.com/directiones/p/10766856.html
Copyright © 2011-2022 走看看