Bitte melden Sie sich an, um dieses Medienelement zu sehen.
Code
- #Grzyb.ovh
- import os
- import shutil
- source_folder = '/txt/src'
- target_folder = '/txt/src2'
- if not os.path.exists(target_folder):
- os.makedirs(target_folder)
- ifdef = '#ifdef '
- ifndef = '#ifndef '
- define = 'ENABLE_SWITCHBOT'
- def process_files(source_folder, target_folder):
- for entry in os.scandir(source_folder):
- if entry.is_file() and entry.name.endswith(('.cpp', '.h')):
- with open(entry.path, 'r', encoding='latin1') as file:
- new_file = None
- copy = False
- lines = file.readlines()
- line_num = 0
- for line in lines:
- if ifdef + define in line or ifndef + define in line:
- copy = True
- if not new_file:
- new_file = open(os.path.join(target_folder, entry.name), 'w', encoding='latin1')
- for i in range(max(0, line_num - 30), line_num):
- new_file.write(lines[i])
- if copy:
- new_file.write(line)
- if '#endif' in line:
- if copy:
- new_file.write("\n // =================== INNA FUNKCJA ================= \n \n")
- new_file.write("// ====== WYSZUKAJ: ")
- new_file.write(define)
- new_file.write("\n// Tutorial wygenerowany automatycznie przez Grzyb.ovh \n")
- new_file.write("\n \n")
- copy = False
- line_num += 1
- if new_file:
- new_file.close()
- elif entry.is_dir():
- new_target_folder = os.path.join(target_folder, entry.name)
- if not os.path.exists(new_target_folder):
- os.makedirs(new_target_folder)
- process_files(entry.path, new_target_folder)
- process_files(source_folder, target_folder)