Python 如何跳过读取csv文件列表的空文件
我有60个csv文件:Python 如何跳过读取csv文件列表的空文件,python,pandas,csv,Python,Pandas,Csv,我有60个csv文件: fname[i] i=0:59 while fname[19] (or maybe some other file) is empty 如何将它们读取到列表中而不出现错误“pandas.errors.EmptyDataError:没有要从文件解析的列” 您将拥有一个文件列表,在读取文件之前,请尝试检查文件是否为空 for file in file_list: if os.stat(file).st_size != 0: # df = pd.re
fname[i] i=0:59
while fname[19] (or maybe some other file) is empty
如何将它们读取到列表中而不出现错误“pandas.errors.EmptyDataError:没有要从文件解析的列” 您将拥有一个文件列表,在读取文件之前,请尝试检查文件是否为空
for file in file_list:
if os.stat(file).st_size != 0:
# df = pd.read_csv(file)
# do your stuf
或者您也可以尝试捕捉错误并忽略。(乔恩·克莱门茨建议)
使用try并捕获异常 try: # read your files except
pandas.errors.EmptyDataError
:
pass
您是否尝试捕获异常并忽略它?是否正在循环读取?在阅读之前,您不能检查文件是否为空。
os.path.getsize(文件)>0
@cᴏʟᴅsᴘᴇᴇᴅ 大于零的文件大小不一定意味着要读取任何内容。。。。例如,创建一个只有空行的文件,它将通过检查,但仍然会抛出异常
try:
# read your files
except pandas.errors.EmptyDataError
:
pass