Advertisement
Jexal

fd27a00a-e51d-46ce-a6d8-5429e062ee91

Mar 3rd, 2025
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. def get_target_directory():
  2.     """
  3.    Prompt the user to enter a valid directory path.
  4.    Keeps prompting until a valid directory path is entered.
  5.    If the specified path does not exist, prompt the user to create it.
  6.    """
  7.     while True:
  8.         target_directory_path = input(Fore.CYAN + "> Enter the target directory's path: ").strip().strip('"')
  9.         if not target_directory_path:
  10.             print(Fore.RED + "> Please enter a valid directory path.\n")
  11.         elif not os.path.isdir(target_directory_path):
  12.             create_dir = input(Fore.YELLOW + f"> The specified path does not exist. Do you want to create it? (y/n): ").strip().lower()
  13.             if create_dir == 'y':
  14.                 try:
  15.                     os.makedirs(target_directory_path)
  16.                     print(Fore.GREEN + f"> Directory created: {target_directory_path}\n")
  17.                     return target_directory_path
  18.                 except Exception as e:
  19.                     print(Fore.RED + f"> Failed to create directory: {target_directory_path}. Error: {str(e)}\n")
  20.             else:
  21.                 print(Fore.RED + "> Please enter a valid directory path.\n")
  22.         else:
  23.             return target_directory_path
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement