使用with自动关闭文件

master
wuhongyewhy 2018-05-01 10:27:27 +08:00 committed by GitHub
parent 688653828a
commit ffebb22b28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 0 deletions

View File

@ -320,6 +320,10 @@
fp.truncate([size]) # 把文件裁成规定的大小,默认的是裁到当前文件操作标记的位置。
for line in open('data'):
print(line) # 使用for语句比较适用于打开比较大的文件
with open('data') as file:
print(file.readline()) # 使用with语句可以保证文件关闭
with open('data') as file:
lines = file.readlines() # 一次读入文件所有行,并关闭文件
open('f.txt', encoding = 'latin-1') # Python3.x Unicode文本文件
open('f.bin', 'rb') # Python3.x 二进制bytes文件
# 文件对象还有相应的属性buffer closed encoding errors line_buffering name newlines等