Min*_*sha 6 python io file python-3.x
当我使用“ 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'>
Run Code Online (Sandbox Code Playgroud)
在代码中,我正在读取一个大文件,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我想念什么?
此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
Run Code Online (Sandbox Code Playgroud)