File size: 1,279 Bytes
9963a74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Import the csv and os modules
import csv
import os

# Define the directory where the csv files are located
directory = "."

# Define a function to validate a csv file
def validate_csv(file):
    # Open the file in read mode
    with open(file, "r", encoding="utf-8") as f:
        # Create a csv reader object
        reader = csv.reader(f)
        # Get the header row
        header = next(reader)
        # Check if the header has the expected number of columns
        if len(header) != 4:
            # Return False if not
            return False
        # Loop through the rest of the rows
        for row in reader:
            # Check if each row has the same number of columns as the header
            if len(row) != len(header):
                # Return False if not
                return False
    # Return True if no errors are found
    return True

# Loop through each file in the directory
for file in os.listdir(directory):
    # Check if the file is a csv file
    if file.endswith(".csv"):
        # Validate the csv file and print the result
        print(f"Validating {file}...")
        result = validate_csv(os.path.join(directory, file))
        if result:
            print(f"{file} is valid.")
        else:
            print(f"{file} is invalid.")