-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_csv.py
54 lines (41 loc) · 1.82 KB
/
merge_csv.py
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
import os
import csv
def merge_csv_files(directory):
# Create a 'processed' folder if it doesn't exist
processed_dir = os.path.join(directory, "processed_photo_details_csvs")
os.makedirs(processed_dir, exist_ok=True)
output_file = os.path.join(processed_dir, "merged_photo_details.csv")
total_rows = 0
first_file = True
with open(output_file, 'w', newline='') as outfile:
writer = None
for filename in os.listdir(directory):
if filename.endswith(".csv"):
file_path = os.path.join(directory, filename)
# Skip the processed folder
if os.path.dirname(file_path) == processed_dir:
continue
with open(file_path, 'r', newline='') as infile:
reader = csv.reader(infile)
if first_file:
# Write headers from the first file
headers = next(reader)
writer = csv.writer(outfile)
writer.writerow(headers)
first_file = False
else:
# Skip header for subsequent files
next(reader)
# Write all rows from this file
for row in reader:
writer.writerow(row)
total_rows += 1
print(f"Processed: {filename}")
if total_rows > 0:
print(f"Merged CSV saved as: {output_file}")
print(f"Total rows in merged CSV: {total_rows}")
else:
print("No CSV files found to merge.")
if __name__ == "__main__":
photos_all_directory = "." # Replace with the path of the directory containing all the random-named .csvs (probably `Photos_All`)
merge_csv_files(photos_all_directory)