nicuf

replace_increment

Jul 2nd, 2024
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import os
  2. import re
  3.  
  4. def get_list_dir(path, depth=False, type='all', inc=True, exclude=[], max=95):
  5.     list = []
  6.     if not path or not os.path.isdir(path):
  7.         return False
  8.  
  9.     base_path = os.getcwd()
  10.     if base_path != path:
  11.         try:
  12.             os.chdir(path)
  13.         except:
  14.             return False
  15.  
  16.     required_path = os.getcwd()
  17.     if not required_path:
  18.         return False
  19.  
  20.     try:
  21.         for entry in os.scandir(required_path):
  22.             if entry.name not in exclude:
  23.                 if type == 'all' or (type == 'dir' and entry.is_dir()) or (type == 'file' and entry.is_file()):
  24.                     list.append(os.path.join(required_path, entry.name) if inc else entry.name)
  25.  
  26.                 if entry.is_dir() and depth:
  27.                     if max < 1:
  28.                         list.append('Too many subdirectories, indexing interrupted.')
  29.                         break
  30.                     else:
  31.                         x = get_list_dir(entry.path, depth, type, inc, exclude, max-1)
  32.                         list.extend(x if x else [])
  33.     finally:
  34.         os.chdir(base_path)
  35.  
  36.     return list
  37.  
  38. # Main script
  39. to_be_replaced = 'wxyz'  # exactly what it wants replaced
  40. nr_start = 1  # from which no start counting
  41. path_files = os.getcwd()
  42.  
  43. excluded_files = [
  44.     '.htaccess', 'robots.txt', '.ftpquota', 'dezabonare.html', 'despre.html',
  45.     'evenimente.html', 'training-si-consultanta.html', 'contact.html', 'despre.html',
  46.     'newsletter.html', 'newsletter_confirm.html', 'feedback.html', 'feedback_thankyou.html',
  47.     'parteneri.html', 'directory.html', 'comanda.html', 'termeni-si-conditii.html',
  48.     'y_key_e479323ce281e459.html', 'yandex_46f3adbe1b25a4ca.html', 'test4.html',
  49.     'search.html', 'inlocuire.php'
  50. ]
  51.  
  52. file_list = get_list_dir(path_files, False, 'file', True, excluded_files)
  53.  
  54. if file_list and isinstance(file_list, list):
  55.     file_list.sort()
  56.     for file in file_list:
  57.         with open(file, 'r', encoding='utf-8') as f:
  58.             original_content = f.read()
  59.  
  60.         if to_be_replaced in original_content:
  61.             content_modified = original_content.replace(to_be_replaced, str(nr_start))
  62.             content_modified = re.sub(r'\n{4,}', '\n\n', content_modified)
  63.  
  64.             try:
  65.                 with open(file, 'w', encoding='utf-8') as f:
  66.                     f.write(content_modified)
  67.             except:
  68.                 print(f'Error: Unable to modify the file {file}. I stayed at number {nr_start}')
  69.                 break
  70.  
  71.             nr_start += 1
  72.  
  73.     print(f'They were checked {len(file_list)} files and the last number is {nr_start - 1}')
  74. else:
  75.     print('Files Not found, check the file path')
Advertisement
Add Comment
Please, Sign In to add comment