如何使用python中的pandas检查csv文件是否为空?

时间:2019-04-05 09:34:54

标签: python csv path

我在一个特定的文件夹中有许多CSV文件。我想检查其中的每个文件是否为空,如果是,请打印其名称。

文件:

 /**
 * Prints indexes 
 * @param character     a character in the range 'A' <= character <= 'Z'
 * @param maxNum        the maximum value to output
 */
public static void printIndexes(char character, int maxNum) {
    int index = character - 64;
    while (index <= maxNum) {
        System.out.println(index);
        index += 26;
    }
}

预期输出:

file1.csv
file2.csv
file3.csv

3 个答案:

答案 0 :(得分:1)

这是您的操作方式,但不能使用熊猫。

for file in file_list:
    if os.stat(file).st_size != 0:
        # df = pd.read_csv(file)
        # do your stuf

另一个例子

import os
if os.stat(file).st_size == 0:
    do something

但是如果您必须对熊猫做这是正确的方法

for file in file_list:
    try:
        # do your stuff
    except pandas.errors.EmptyDataError:
        print "Found empty file : {file}".format(file=file)

所有示例中的代码都是不言自明的。

答案 1 :(得分:1)

您可以使用python os库。您不需要熊猫。

例如:

import os

os.path.getsize('yourfile.csv')

如果文件大小等于0,则为空。

答案 2 :(得分:0)

import os
os.stat("file").st_size == 0