zoukankan      html  css  js  c++  java
  • 文件读取

    # 参数1 文件路径  可以是相对 也可以是绝对
    # 参数2 mode  打开文件的模式
    #    r(只读)
    #    w(只写)
    
    #    + (可读可写) 了解即可
    # 参数3 encoding 编码方式  windows默认为GBK linux默认为UTF-8
    
    f = open(r"D:sh_fullstack_s6day8代码	est.txt",mode="r",encoding="utf-8")
    # # 读取内容 可以用参数指定要读取的个数,默认为-1 表示全部读取
    # # 需要注意 read(-1) 仅限于文件较小时,如果文件太大会造成内存溢出
    data = f.read()
    print(data)
    #
    # # print(f.readable()) # 判断是否可读
    # # print(f.writable()) # 判断是否可写
    #
    # line = f.readline() # 读取一行
    # print(line)
    #
    # # line2 = f.readline()
    # # print(line2)
    f.close()
    
    # 使用循环 来读取全部内容 方式1
    # with open(r"D:sh_fullstack_s6day8代码	est.txt",mode="r",encoding="utf-8") as f:
    #     while True:
    #         line = f.readline()
    #         if not line: # 如果line为空则表示没有内容了
    #             print("
    没有内容啦!")
    #             break
    #         print(line,end="")
    
    # 使用循环 来读取全部内容 方式2
    # with open(r"D:sh_fullstack_s6day8代码	est.txt",mode="r",encoding="utf-8") as f:
    #     for line in f:
    #         print(line,end="")
    
    # 一次性读取全部
    # with open(r"D:sh_fullstack_s6day8代码	est.txt",mode="r",encoding="utf-8") as f:
    #     print(f.readlines()) # 会将每一行放入列表中
    
    
    """读取相关函数
    read()   读取全部
    read(size)  读取指定大小
    readlines   读取全部
    readline   读取一行
    readable   是否可读
    """
  • 相关阅读:
    mysql 时间戳
    css优先级
    app横竖屏切换
    文本溢出时显示省略号
    react页面间传递参数
    去掉input获取focus时的边框
    Mac中好用的快捷键
    python 图片处理
    CSS padding margin border属性详解
    python3.x执行post请求时报错“POST data should be bytes or an iterable of bytes...”的解决方法
  • 原文地址:https://www.cnblogs.com/Hale-wang/p/10354227.html
Copyright © 2011-2022 走看看