我在一个特定的文件夹中有很多CSV文件。 因此,我只想检查哪个CSV文件为空,如果任何csv文件为空,则也要打印该文件名。 并且某些文件可能只包含标题部分。因此,此类型的文件也将是空文件。我还需要打印该文件。
try:
path = '/nsmnt/NS_Exec_DSHBD/output/*.csv'
files = glob.glob(path)
for name in files:
with open(name, 'r') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
#print(row)
if row[0] == 'NULL':
print("file is empty!")
print(name)
except Exception as ex:
print(ex)
答案 0 :(得分:1)
问题归结为如何确定文件是否最多具有一行(这是标题)。
def is_empty_csv(path):
with open(name) as csvfile:
reader = csv.reader(csvfile)
for i, _ in enumerate(reader):
if i: # found the second row
return False
return True