Created
May 5, 2021 16:59
-
-
Save royerk/2721e04722be488dd43126ec9e687aa8 to your computer and use it in GitHub Desktop.
Python script to merge python scripts together
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
files_to_include = [ | |
'my_class.py', | |
'dir/other_file.py', | |
'main.py' # << finish by main | |
] | |
output_file = 'bundled_code.py' | |
if __name__ == "__main__": | |
all_code = [] | |
for file_name in files_to_include: | |
with open(file_name, 'r') as file: | |
code = file.readlines() | |
# here you can filter lines with a regex: some_lines = re.findall('my-regex', code) | |
# for example remove all lines containing imports from another file: | |
ignore = [] | |
for file_name in files_to_include: | |
# get the file name without path or extension | |
file_name_no_dir_no_extension = file_name.split('.py')[0].split('/')[-1] | |
for line_of_code in code: | |
# detect lines containing imports | |
if re.findall(f'(import|from).*{file_name_no_dir_no_extension}.*', line_of_code): | |
ignore+= [line_of_code] | |
code = [line for line in code if line not in ignore] | |
all_code += code | |
with open(output_file, 'w+') as file: | |
file.write(''.join(all_code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment