Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import filedialog
- import shutil
- from datetime import datetime
- def list_files_and_organize_by_year(folder_path):
- print("Listing files by date modified and organizing by year...")
- # Get a list of files in the folder
- files = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, filename))]
- # Create a dictionary to track the season numbers for each year
- year_counts = {}
- # Traverse the folder tree and list files by date modified
- for root, _, files in os.walk(folder_path):
- for file_name in files:
- file_path = os.path.join(root, file_name)
- year = get_modification_year(file_path)
- if year:
- # Create a folder for the year if it doesn't exist
- year_folder = os.path.join(folder_path, str(year))
- os.makedirs(year_folder, exist_ok=True)
- # Move the file to the corresponding year folder
- shutil.move(file_path, os.path.join(year_folder, file_name))
- print(f"Moved: {file_name} to {year}")
- print("Files listed by date modified and organized by year.")
- organize_folders_as_seasons(folder_path)
- def organize_folders_as_seasons(folder_path):
- print("Organizing folders as seasons...")
- # Get a list of folders in the parent folder
- subfolders = [f for f in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, f))]
- # Sort the folders by their names (years)
- subfolders.sort()
- # Rename the folders as "Season 1," "Season 2," and so on
- for idx, subfolder in enumerate(subfolders, start=1):
- season_name = f"Season {idx}"
- season_path = os.path.join(folder_path, subfolder)
- os.rename(season_path, os.path.join(folder_path, season_name))
- print(f"Renamed folder: {subfolder} to {season_name}")
- print("Folders organized as seasons.")
- rename_files_in_seasons(folder_path)
- def rename_files_in_seasons(folder_path):
- print("Renaming files in each season folder...")
- # Get a list of folders (seasons)
- seasons = [f for f in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, f))]
- # Sort the seasons by their names (Season 1, Season 2, ...)
- seasons.sort()
- # Loop through each season folder
- for season in seasons:
- season_folder = os.path.join(folder_path, season)
- files = [os.path.join(season_folder, filename) for filename in os.listdir(season_folder) if os.path.isfile(os.path.join(season_folder, filename))]
- # Sort files by date modified in ascending order
- files.sort(key=lambda x: os.path.getmtime(x))
- # Rename files in the format sXXeYY
- for idx, file_path in enumerate(files, start=1):
- season_episode = f"s{season.split(' ')[-1].zfill(2)}e{idx:02d}"
- _, file_extension = os.path.splitext(file_path)
- new_file_name = f"{season_episode}{file_extension}"
- os.rename(file_path, os.path.join(season_folder, new_file_name))
- print(f"Renamed: {file_path} to {new_file_name}")
- print("Files renamed in each season folder.")
- def get_modification_year(file_path):
- try:
- # Get the file's modification timestamp
- modification_timestamp = os.path.getmtime(file_path)
- # Convert the timestamp to a datetime object
- modification_date = datetime.fromtimestamp(modification_timestamp)
- # Extract the year from the datetime object
- return modification_date.year
- except Exception as e:
- print(f"Error getting modification year for {file_path}: {str(e)}")
- return None
- def main():
- root = tk.Tk()
- root.withdraw() # Hide the main tkinter window
- folder_path = filedialog.askdirectory(title="Select Folder")
- if folder_path:
- list_files_and_organize_by_year(folder_path)
- # Add an input prompt to wait for user input before closing the console window
- input("Press Enter to exit...")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment