Python with子句
with子句
with语句是用来简化代码的,在将打开文件的操作放在wirh语句中,代码块结束后,文件将自动关闭
>>> with open('/etc/hosts') as f: ... data = f.read() ... >>> f.closed True
with结束文件自动关闭
>>> with open('/etc/hosts') as f: ... data = f.read() ... >>> f.closed True
with结束文件自动关闭