[英]Python 3: ResourceWarning: unclosed file <_io.TextIOWrapper name='PATH_OF_FILE'
When I run the test cases in python with "python normalizer/setup.py test " I am getting the below exception 当我使用“python normalizer / setup.py test”在python中运行测试用例时,我得到以下异常
ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/workspace/aiworkspace/skillset-normalization-engine/normalizer/lib/resources/skills.taxonomy' mode='r' encoding='utf-8'>
In code I am reading a big file like below: 在代码中,我正在阅读如下的大文件:
def read_data_from_file(input_file):
current_dir = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
file_full_path = current_dir+input_file
data = open(file_full_path,encoding="utf-8")
return data
What am I missing? 我错过了什么?
From Python unclosed resource: is it safe to delete the file? 从Python unclosed resource:删除文件是否安全?
This ResourceWarning means that you opened a file, used it, but then forgot to close the file.
此ResourceWarning意味着您打开了一个文件,使用它,但后来忘记关闭该文件。 Python closes it for you when it notices that the file object is dead, but this only occurs after some unknown time has elapsed.
当Python注意到文件对象已经死亡时,Python会为你关闭它,但这只会在经过一段时间后才会发生。
def read_data_from_file(input_file):
current_dir = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
file_full_path = current_dir+input_file
with open(file_full_path, 'r') as f:
data = f.read()
return data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.