Par-Four-Fineweb-Edu-Fortified / organize_by_folder.py
Josephgflowers's picture
Upload 3 files
872030d verified
import os
import shutil
# Define the years and the base directory
years = ["2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024"]
base_dir = '/media/joe/512-3/csv' # Adjust this to your actual base directory
# Step 1: Create directories for each year
for year in years:
folder_path = os.path.join(base_dir, f"CC-MAIN-{year}")
if not os.path.exists(folder_path):
os.makedirs(folder_path)
print(f"Created folder: {folder_path}")
# Step 2: Move files to their respective folders and rename them
for year in years:
year_folder = os.path.join(base_dir, f"CC-MAIN-{year}")
# Find all files that match the pattern for this year
year_files = [f for f in os.listdir(base_dir) if f.startswith(f"forti-sampled_dataset_CC-MAIN-{year}")]
total_files = len(year_files)
if total_files == 0:
print(f"No files found for the year {year}")
continue
# Sort and rename the files
for index, file_name in enumerate(sorted(year_files)):
new_file_name = f"train-{str(index+1).zfill(5)}-of-{str(total_files).zfill(4)}.csv"
old_file_path = os.path.join(base_dir, file_name)
new_file_path = os.path.join(year_folder, new_file_name)
# Attempt to move and rename the file
try:
shutil.move(old_file_path, new_file_path)
print(f"Moved and renamed: {old_file_path} to {new_file_path}")
except Exception as e:
print(f"Error moving file {old_file_path}: {e}")