报错信息:
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
原因分析: 缺少close()
解决方案:
with open(testReportDir_FileName, mode="r", encoding="utf-8") as f:
data_file = f.read()
with open的用途:是python用来打开本地文件的,它会在使用完毕后,自动关闭文件,无需手动书写close()