Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- def get_list_dir(path, depth=False, type='all', inc=True, exclude=[], max=95):
- list = []
- if not path or not os.path.isdir(path):
- return False
- base_path = os.getcwd()
- if base_path != path:
- try:
- os.chdir(path)
- except:
- return False
- required_path = os.getcwd()
- if not required_path:
- return False
- try:
- for entry in os.scandir(required_path):
- if entry.name not in exclude:
- if type == 'all' or (type == 'dir' and entry.is_dir()) or (type == 'file' and entry.is_file()):
- list.append(os.path.join(required_path, entry.name) if inc else entry.name)
- if entry.is_dir() and depth:
- if max < 1:
- list.append('Too many subdirectories, indexing interrupted.')
- break
- else:
- x = get_list_dir(entry.path, depth, type, inc, exclude, max-1)
- list.extend(x if x else [])
- finally:
- os.chdir(base_path)
- return list
- # Main script
- to_be_replaced = 'wxyz' # exactly what it wants replaced
- nr_start = 1 # from which no start counting
- path_files = os.getcwd()
- excluded_files = [
- '.htaccess', 'robots.txt', '.ftpquota', 'dezabonare.html', 'despre.html',
- 'evenimente.html', 'training-si-consultanta.html', 'contact.html', 'despre.html',
- 'newsletter.html', 'newsletter_confirm.html', 'feedback.html', 'feedback_thankyou.html',
- 'parteneri.html', 'directory.html', 'comanda.html', 'termeni-si-conditii.html',
- 'y_key_e479323ce281e459.html', 'yandex_46f3adbe1b25a4ca.html', 'test4.html',
- 'search.html', 'inlocuire.php'
- ]
- file_list = get_list_dir(path_files, False, 'file', True, excluded_files)
- if file_list and isinstance(file_list, list):
- file_list.sort()
- for file in file_list:
- with open(file, 'r', encoding='utf-8') as f:
- original_content = f.read()
- if to_be_replaced in original_content:
- content_modified = original_content.replace(to_be_replaced, str(nr_start))
- content_modified = re.sub(r'\n{4,}', '\n\n', content_modified)
- try:
- with open(file, 'w', encoding='utf-8') as f:
- f.write(content_modified)
- except:
- print(f'Error: Unable to modify the file {file}. I stayed at number {nr_start}')
- break
- nr_start += 1
- print(f'They were checked {len(file_list)} files and the last number is {nr_start - 1}')
- else:
- print('Files Not found, check the file path')
Advertisement
Add Comment
Please, Sign In to add comment