nicuf

increment html files

Jun 3rd, 2024 (edited)
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import os
  2. import re
  3.  
  4. # Directorul unde se află fișierele HTML
  5. directory = r'e:\output'
  6.  
  7. # Valoarea inițială a lui item_id
  8. start_id = 6730
  9.  
  10. # Pattern-ul regex pentru identificarea liniei cu $item_id
  11. pattern = re.compile(r'<!-- \$item_id = (\d+); // Replace that with your rating id -->')
  12.  
  13. # Obține lista de fișiere HTML din director
  14. html_files = [f for f in os.listdir(directory) if f.endswith('.html')]
  15.  
  16. # Sortează fișierele pentru a se asigura că incrementul se face în ordinea corectă
  17. html_files.sort()
  18.  
  19. # Incrementează și actualizează valoarea lui $item_id în fiecare fișier
  20. for i, filename in enumerate(html_files):
  21.     file_path = os.path.join(directory, filename)
  22.     with open(file_path, 'r', encoding='utf-8') as file:
  23.         content = file.read()
  24.  
  25.     match = pattern.search(content)
  26.     if match:
  27.         old_id = int(match.group(1))
  28.         new_id = start_id + i
  29.         new_content = pattern.sub(f'<!-- $item_id = {new_id}; // Replace that with your rating id -->', content)
  30.  
  31.         with open(file_path, 'w', encoding='utf-8') as file:
  32.             file.write(new_content)
  33.         print(f'Updated {filename} with $item_id = {new_id}')
  34.     else:
  35.         print(f'No match found in {filename}')
  36.  
  37. print("All files have been updated successfully.")
Advertisement
Add Comment
Please, Sign In to add comment