reblue520
# coding=utf-8

def open_file():
    """使用with打开一个文件"""

    # 普通操作文件方法
    # f = open(\'./static/hello.txt\', \'r\', encoding=\'utf-8\')
    # rest = f.read()
    # print(rest)
    # f.close()

    # with 语法
    # with open(\'./static/hello.txt\', \'r\', encoding=\'utf-8\') as f:
    #     rest = f.read()
    #     print(rest)

    # with 语法内部相当于如下代码
    try:
        f = open(\'./static/hello.txt\', \'r\', encoding=\'utf-8\')
        rest = f.read()
        print(rest)
    except:
        pass
    finally:
        f.close()


if __name__ == "__main__":
    open_file()

 

分类:

技术点:

相关文章:

  • 2021-11-09
  • 2021-06-15
  • 2021-12-19
  • 2021-10-06
  • 2021-12-09
  • 2019-12-18
  • 2021-02-20
猜你喜欢
  • 2021-12-04
  • 2021-12-14
  • 2021-11-23
  • 2021-10-29
  • 2021-12-24
  • 2021-12-04
  • 2021-09-06
相关资源
相似解决方案