2020年1月14日
|
by YoungTimes
|
No comments
Python-建议在with语句(with statement)中执行文件操作
1、为什么要在with语句中执行文件操作
常见的python文件读写流程如下:首先使用open()函数打开文件,open()函数返回文件对象(file object),然后使用该文件对象(file object)进行文件读写,最后在程序结束前执行文件关闭(close()函数)文件。
示例程序如下:
# open a file
file_object = open('sample.txt')
# read the file content
data = file_object.read()
# print file content
print(data)
#close the file
file_object.close()
当python程序的同级目录存在sample.txt文件时,程序正常执行,打开文件成功,并打印sample.txt文件中的内容:
This is a sample file.
It contains some sample string.
you are going to use it.
Thanks.
当文件打开失败(如文件不存在时等),程序可能会输出以下错误信息:
FileNotFoundError: [Errno 2] No such file or directory: 'sample.txt'
上面的代码在大部分场景下可以正常工作,但是在一些场景下会有问题。
1)如果代码末尾忘记了关闭文件怎么办?
在一个大的工程项目中,这种情况很可能发生,因为在打开文件时,我们往往要做一大堆事情,比如各种条件检测等,所以可能在执行到close()函数之前,程序就已经返回。
在这种场景下,我们就没有调用close()函数,所以在函数return之后,文件仍然是打开的,它仍然在进程中占用大量的内存空间,并且还可能由于部分内存数据未写入文件导致数据丢失。
2)如果程序中发生异常怎么办?
如下代码,我们刻意在代码中制造了一个异常,这种场景下会发生什么呢?
# File is not closed in case of exception
try:
# open a file
file_object = open('sample.txt')
# read file content
data = file_object.read()
# It will raise an exception
x = 1 / 0
print(data)
file_object.close()
except:
# Handling the exception
print('An Error')
finally:
if file_object.closed == False:
print('File is not closed')
else:
print('File is closed')
上述程序的输出结果:
An Error
File is not closed
可以看到,上述代码中,由于执行过程中发生了异常(except),代码在执行close()函数之前就跳转到异常(except)处理流程,最后执行到finally流程。由于异常(except)的发生改变了代码的执行流程,close()函数未能执行,所有已经打开的文件也未能正常关闭。
当然,可以在异常(except)处理流程或者finally流程中调用close()函数来解决这个Bug,但是重复的代码不是一个优雅的解决方案。
# File is not closed in case of exception
try:
# open a file
file_object = open('sample.txt')
# read file content
data = file_object.read()
# It will raise an exception
x = 1 / 0
print(data)
file_object.close()
except:
file_object.close()
# Handling the exception
print('An Error')
finally:
if file_object.closed == False:
print('File is not closed')
else:
print('File is closed')
如何让我们系统自动帮我们实现资源的回收呢?with语句可以帮助我们解决这一问题。
2、如何在with语句中打开文件
在with语句中打开文件的示例代码如下:
using "with statement" with open() function
with open('sample.txt', "r") as file_object:
# read file content
data = file_object.read()
# print file contents
print(data)
# Check if file is closed
if file_object.closed == False:
print('File is not closed')
else:
print('File is closed')
程序输出:
This is a sample file.
It contains some sample string.
you are going to use it.
Thanks.
File is closed
with语句创建了一个execution block,block内创建的所有对象在退出execution block后会自动销毁或者关闭。因此在with语句的execution block执行结束后,文件的close()函数会被自动调用,也就不用我们手动调用close()函数,也不必在代码中担心文件没有关闭的问题。
3、在with语句内执行文件读写的好处
1.减少由于代码错误出bug的几率
由于不需要开发人员再处处关注文件是否关闭,在with语句的execution block执行完毕后,文件对象的close()函数会被自动调用,代码量减少了,也降低了bug出现的概率。
2.优雅的处理程序异常(exception)
再看下异常处理的代码:
# File will be closed before handling the exception
try:
# using "with statement" with open() function
with open('sample.txt', "r") as file_object:
# read file content
data = file_object.read()
# it raises an error
x = 1 / 0
print(data)
except:
# handling exception
print('An Error')
if file_object.closed == False:
print('File is not closed')
else:
print('File is closed')
程序输出:
An Error
File is closed
可以看到,如果我们在with语句(with statement)中执行了文件打开(open)的操作,即使在with的execution block中出现了异常,在代码执行流程切换到异常处理模块(except block)前,系统自动执行清理操作(文件关闭等)。因此在使用with语句后,即使出现了异常,我们也不必自己操心文件关闭等资源清理工作。
4. 在单个with语句中同时打开多个文件
当在with语句(with statement)中打开多个文件时,当with语句的execution block执行结束,两个文件的close()函数都会被自动调用,它们占用的资源都会被关闭清理。
示例代码如下:
# Read from sample.txt and write in outfile.txt
with open('outfile.txt', 'w') as file_obj_2, open('sample.txt', 'r') as file_obj_1:
data = file_obj_1.read()
file_obj_2.write(data)
# Both the files will be closed automatically when execution block ends.
参考链接:
除非注明,否则均为[半杯茶的小酒杯]原创文章,转载必须以链接形式标明本文链接