""" Author: Feyza Sakin Date: 02/08/2024 Summary: With the given path, it puts all the csv files into a new folder named "new_folder_" with the timestamp (e.g. "new_folder_2024-02-07_14-30-22") Input: fcltls move_csv_new_folder 'folder path containing CSV files' Output: 1. The following code gets the path. 2. Gets all the CSV files in the path. 3. Creates a new folder 4. Moves all the CSV files into the folder. """ import os import glob import click from loguru import logger from src import click_config_file,myprovider import datetime import shutil logger.trace(f"After imports {__file__}") @click.command() @click.argument('folder_path', type=click.Path(exists=True)) @click_config_file.configuration_option(implicit=True,provider=myprovider) def cli(folder_path): """ Command to put all the CSV files into a new folder """ logger.debug("Entering scan") # # Checks if there's already a folder named "base_name" # If there's, it increase the naming to have a unique name def generating_unique_folder_name(base_name): # Initialize a counter. This number will be increased and used in the naming if the new created folder is taken counter = 1 # Set the file_name to the name of the folder file_name = f"{base_name}" # Look to the directory to check if there's already a folder with the input name while os.path.exists(file_name): # If the file name exists, then increase the number to the base name file_name = f"{base_name}_{counter}" # Increment the counter counter += 1 return file_name # # # If the path to the directory is not given, then return message if not folder_path: click.echo("\nPlease provide the folder path.\n") return # Put all the CSV files' paths into the "csv_files" list csv_files = glob.glob(os.path.join(folder_path, '*.csv')) # If csv_files is empty, then tell the user that there's no csv file if not csv_files: click.echo(f"\nNo CSV files found in {folder_path}.\n") # If there's any csv file, create a new folder and move all the csv files into the new folder else: # Create a new folder in the same directory. # # Get the current date and time folder_created_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") # # Create a folder name called "new_folder_" with time new_created_directory_name = "new_folder_" + folder_created_time; # # Using "generating_unique_folder_name" function, create a unique folder name in the directory new_created_directory_name = generating_unique_folder_name(new_created_directory_name); click.echo(f"\nThe name of the new folder is {new_created_directory_name}."); # # Add the name of the created directory's name to the path new_dirs_path = os.path.join(folder_path, new_created_directory_name) # Create the directory using "os.makedirs" with an input, which is the path and the name of the new folder's name os.makedirs(new_dirs_path) click.echo(f"The new folder {new_created_directory_name} is created successfully.") # Print success message of the new folder's name # # Put all the csv files into the new folder for a_csv_file in csv_files: # Move the content of source, which is "a_csv_file", to destination, which is "new_dirs_path" shutil.move(a_csv_file, new_dirs_path); click.echo(f"All the csv files in the directory are moved to the new folder {new_created_directory_name}."); # Put all the CSV files' paths into the "new_folder_s_csv_files" list new_folder_s_csv_files = glob.glob(os.path.join(new_dirs_path, '*.csv')); # Check if files are moved to the new folder. # If not, tell the user if not new_folder_s_csv_files: click.echo(f"\nNo CSV files found in the new folder {new_created_directory_name}.") # If files are moved, tell the number of moved files and the name of the moved files click.echo(f"\nSummary:"); click.echo(f"-------------------------------------------------------------"); click.echo(f"* The name of the new folder: \n {new_created_directory_name}"); click.echo(f"* The number of moved files: \n {len(new_folder_s_csv_files)}"); click.echo("* The name of the moved files:"); for a_moved_csv_file_path in new_folder_s_csv_files: # Parse the moved file's name from the path source_directory_path, a_moved_csv_file_name_new = os.path.split(a_moved_csv_file_path) click.echo(f" {a_moved_csv_file_name_new}"); click.echo(""); if __name__ == '__main__': cli() # pylint: disable=no-value-for-parameter