Advertisement
BERKYT

recurce finder files

Feb 26th, 2024 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. import os
  2. import random
  3.  
  4. BASE_DIR = os.path.dirname(__file__)
  5. TARGETS = ['mp4', 'avi', 'mov']
  6. SAVE_DIR = '.__DATA__'
  7. FILTERS = [SAVE_DIR]
  8. IGNORE_DUPLICATE_FILES = True
  9.  
  10. if not os.path.exists(SAVE_DIR):
  11.     os.mkdir(SAVE_DIR)
  12.  
  13.  
  14. def check_file_in_filters(file_name: str):
  15.     if file_name in FILTERS:
  16.         return True
  17.     return False
  18.  
  19.  
  20. def copy_file(file: str, file_name: str):
  21.     with open(file, mode='rb') as f_in, open(f'{BASE_DIR}\\{SAVE_DIR}\\{file_name}', mode='ab') as f_out:
  22.         for line in f_in:
  23.             f_out.write(line)
  24.  
  25.  
  26. def get_file_extension(file_name: str):
  27.     return file_name[file_name.rfind('.')+1:]
  28.  
  29.  
  30. def search(path=BASE_DIR):
  31.     try:
  32.         if os.path.isdir(path):
  33.             lst_dir = os.listdir(path)
  34.         else:
  35.             return
  36.     except PermissionError as _:
  37.         return
  38.  
  39.     for file_name in lst_dir:
  40.         if check_file_in_filters(file_name):
  41.             continue
  42.  
  43.         if os.path.isdir(f'{path}\\{file_name}'):
  44.             search(f'{path}\\{file_name}')
  45.         else:
  46.             print(f'Checking file: {path}\\{file_name}')
  47.             if get_file_extension(file_name) in TARGETS:
  48.  
  49.                 if os.path.exists(f'{BASE_DIR}\\{SAVE_DIR}\\{file_name}'):
  50.                     if IGNORE_DUPLICATE_FILES:
  51.                         continue
  52.                     else:
  53.                         file_name = f'{file_name}_{str(random.random())}'
  54.  
  55.                 print('Coping...')
  56.                 copy_file(f'{path}\\{file_name}', file_name)
  57.                 print(f'{path}\\{file_name} copied!')
  58.             else:
  59.                 print('Not suitable!')
  60.  
  61.  
  62. search()
  63. print('Done.')
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement