简体   繁体   English

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

[英]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.

相关问题 莫名其妙的ResourceWarning:未关闭的文件&lt;_io.TextIOWrapper名称= 3 - Inexplicable ResourceWarning: unclosed file <_io.TextIOWrapper name=3 ResourceWarning:未关闭的文件 &lt;_io.BufferedReader name=4&gt; - ResourceWarning: unclosed file <_io.BufferedReader name=4> Python3:Reportlab图像 - ResourceWarning:未闭合文件&lt;_io.BufferedReader name = ...&gt; - Python3: Reportlab Image - ResourceWarning: unclosed file <_io.BufferedReader name=…> 未关闭但 UnitTest 正在抛出它的文件的 ResourceWarning - ResourceWarning for a file that is unclosed but UnitTest is throwing it 将信息添加到文本文件,TypeError:&#39;_io.TextIOWrapper&#39;对象不可下标 - Adding information to a text file, TypeError: '_io.TextIOWrapper' object is not subscriptable AttributeError: &#39;_io.TextIOWrapper&#39; 对象没有 txt 文件的属性 &#39;lower&#39; - AttributeError: '_io.TextIOWrapper' object has no attribute 'lower' for txt file glob error &lt;_io.TextIOWrapper name =&#39;...&#39;mode =&#39;r&#39;scoding =&#39;cp1252&#39;&gt;读取文本文件错误 - glob error <_io.TextIOWrapper name='…' mode='r' encoding='cp1252'> reading text file error 在 python 中读取 dat 文件显示 UnicodeDecodeError (File<class '_io.TextIOWrapper'> ) - read dat file in python shows UnicodeDecodeError (File <class '_io.TextIOWrapper'>) 如何从文件中删除&lt;_io.TextIOWrapper name =&#39;xyz.txt&#39;mode =&#39;w&#39;encoding =&#39;UTF-8&#39;&gt;? - How to remove <_io.TextIOWrapper name='xyz.txt' mode='w' encoding='UTF-8'> from file? 如何读取 Python json 文件?AttributeError: '_io.TextIOWrapper' object 没有属性 'load' - How to read Python json file?AttributeError: '_io.TextIOWrapper' object has no attribute 'load'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM