File size: 938 Bytes
872030d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pandas as pd

# Define the directory path containing the CSV files
csv_directory = '/media/joe/512-3/csv'

# Define the column names to be added
column_names = ['score', 'text', 'url']

# Function to add headers to CSV files
def add_headers_to_csv(file_path):
    try:
        # Load the CSV file without headers
        df = pd.read_csv(file_path, header=None)

        # Add column names
        df.columns = column_names

        # Save the updated CSV file with headers
        df.to_csv(file_path, index=False)

        print(f"Headers added to '{file_path}'")

    except Exception as e:
        print(f"Error processing '{file_path}': {e}")

# Loop through all the CSV files in the specified directory
for file_name in os.listdir(csv_directory):
    if file_name.endswith('.csv'):
        file_path = os.path.join(csv_directory, file_name)
        add_headers_to_csv(file_path)

print("All CSV files processed.")