繁体   English   中英

Python 3:ResourceWarning:unclosed文件<_io.TextIOWrapper name ='PATH_OF_FILE'

[英]Python 3: ResourceWarning: unclosed file <_io.TextIOWrapper name='PATH_OF_FILE'

当我使用“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'>

在代码中,我正在阅读如下的大文件:

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

我错过了什么?

Python unclosed resource:删除文件是否安全?

此ResourceWarning意味着您打开了一个文件,使用它,但后来忘记关闭该文件。 当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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM