zoukankan      html  css  js  c++  java
  • The 'with' and 'as' Keywords

    Programming is all about getting the computer to do the work. Is there a way to get Python to automatically close our files for us?

    Of course there is. This is Python.

    You may not know this, but file objects contain a special pair of built-in methods: __enter__() and __exit__(). The details aren't important, but what is important is that when a file object's __exit__() method is invoked, it automatically closes the file. How do we invoke this method? With with and as.

    The syntax looks like this:

    with open("file", "mode") as variable:
        # Read or write to the file
    
  • 相关阅读:
    vue 开发系列 企业微信整合
    MongoDB基础3
    MongoDB基础2
    MongoDB基础1
    SpringBoot MongoDB
    UWSGI
    Nginx
    编译python源码
    Flask部署
    Django个人工作总结
  • 原文地址:https://www.cnblogs.com/yaos/p/14014374.html
Copyright © 2011-2022 走看看