ResourceWarning: unclosed file

报错信息:

ResourceWarning: unclosed file <_io.TextIOWrapper name='······' mode='r' encoding='utf-8'>
	  file = get_data(file_name='······')

问题代码:

def get_data(file_name):
    rows = []
    testReportDir = "../test/"
    testReportDir_FileName = testReportDir + file_name
    data_file = open(testReportDir_FileName, mode="r", encoding="utf-8")
    reader = csv.reader(data_file)
    next(reader, None)
    for row in reader:
        rows.append(row)
    return rows

解决方案:

with open(testReportDir_FileName, mode="r", encoding="utf-8") as f:
        data_file = f.read()

你可能感兴趣的:(python_selenium,python)