Created
February 3, 2025 11:35
-
-
Save unstppbl/694fde2517f7fd7f78d35d69ec592e4b to your computer and use it in GitHub Desktop.
python script to combine all .go files of the project into one file
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 os | |
def merge_files_in_folder(folder_path, output_file): | |
""" | |
Merges all .go files in the specified folder and its subfolders into a single file, excluding the vendor directory. | |
Args: | |
folder_path (str): Path to the folder containing .go files to merge. | |
output_file (str): Path to the output file where content will be merged. | |
""" | |
with open(output_file, 'w', encoding='utf-8') as outfile: | |
for root, _, files in os.walk(folder_path): | |
if 'vendor' in root.split(os.sep): | |
continue | |
for file in files: | |
if file.endswith('.go'): | |
file_path = os.path.join(root, file) | |
try: | |
with open(file_path, 'r', encoding='utf-8') as infile: | |
outfile.write(f"\n--- Start of {file_path} ---\n") | |
outfile.write(infile.read()) | |
outfile.write(f"\n--- End of {file_path} ---\n") | |
except Exception as e: | |
print(f"Could not read {file_path}: {e}") | |
if __name__ == "__main__": | |
folder_path = input("Enter the folder path to merge .go files: ").strip() | |
output_file = input("Enter the output file path: ").strip() | |
if not os.path.isdir(folder_path): | |
print(f"The folder path {folder_path} does not exist or is not a directory.") | |
else: | |
merge_files_in_folder(folder_path, output_file) | |
print(f"All .go files in {folder_path} have been merged into {output_file}.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made to import the file into LLMs (chatgpt, gemini, etc)