Par-Four-Fineweb-Edu-Fortified / add-column-names.py
Josephgflowers's picture
Upload 3 files
872030d verified
raw
history blame
938 Bytes
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.")