-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathvalidate_data.py
More file actions
65 lines (51 loc) · 1.91 KB
/
validate_data.py
File metadata and controls
65 lines (51 loc) · 1.91 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import csv
# Dynamically build absolute paths
base_path = os.path.dirname(os.path.abspath(__file__))
csv_files = [
os.path.join(base_path, "data", "personnel.csv"),
os.path.join(base_path, "data", "units.csv"),
os.path.join(base_path, "data", "locations.csv"),
os.path.join(base_path, "data", "equipment.csv"),
os.path.join(base_path, "data", "supplies.csv"),
os.path.join(base_path, "data", "missions.csv")
]
def validate_csv(file_path: str) -> bool:
"""
Validates if a given CSV file exists, is not empty, and is readable.
Args:
file_path (str): The absolute path to the CSV file.
Returns:
bool: True if the file is valid, False otherwise.
"""
if not os.path.exists(file_path):
print(f"Error: {file_path} does not exist.")
return False
if os.path.getsize(file_path) == 0:
print(f"Error: {file_path} is empty.")
return False
if not os.access(file_path, os.R_OK):
print(f"Error: No read permissions for {file_path}.")
return False
try:
with open(file_path, 'r', newline='') as f:
# Attempt to read the first line or use csv.reader to check format
csv.reader(f).__next__() # Try to read the first row to validate CSV format
print(f"{file_path} is OK.")
return True
except csv.Error as e:
print(f"Error: {file_path} has a malformed CSV format: {e}")
return False
except Exception as e:
print(f"Error: Could not process {file_path}: {e}")
return False
if __name__ == "__main__":
print("--- Validating Data Files ---")
all_present = True
for file in csv_files:
if not validate_csv(file):
all_present = False
if all_present:
print("\nAll essential data files are present.")
else:
print("\nWarning: Some essential data files are missing. Please check the errors above.")