当我在 python 中运行测试用例时“python 规范化器/setup.py 测试
”我收到以下异常
ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/workspace/aiworkspace/skillset-normalization-engine/normalizer/lib/resources/skills.taxonomy' mode='r' encoding='utf-8'>
在代码中我正在读取一个大文件,如下所示:
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
我缺少什么?
From Python未关闭资源:删除文件安全吗? https://stackoverflow.com/questions/21057942/python-unclosed-resource-is-it-safe-to-delete-the-file
此 ResourceWarning 意味着您打开了一个文件并使用了它,但随后忘记关闭该文件。当 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)