-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatenate.py
41 lines (32 loc) · 1.44 KB
/
concatenate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import glob
def concatenate_python_files(folder_path, output_file="combined_code.txt"):
"""
Concatenates all Python files in a folder into a single text file.
Args:
folder_path: The path to the folder containing the Python files.
output_file: The name of the output text file (default: "combined_code.txt").
"""
if not os.path.isdir(folder_path):
print(f"Error: '{folder_path}' is not a valid directory.")
return
python_files = glob.glob(os.path.join(folder_path, "*.py"))
if not python_files:
print(f"No Python files found in '{folder_path}'.")
return
try:
with open(output_file, "w", encoding="utf-8") as outfile:
for file_path in python_files:
try:
with open(file_path, "r", encoding="utf-8") as infile:
outfile.write(f"----- File: {file_path} -----\n")
outfile.write(infile.read())
outfile.write("\n\n")
except UnicodeDecodeError:
print(f"Warning: Could not decode file {file_path} using utf-8, skipping this file")
print(f"Successfully concatenated {len(python_files)} Python files into '{output_file}'.")
except Exception as e:
print(f"An error occurred: {e}")
# Example usage:
folder_to_scan = "./" # Replace with the actual folder path if needed.
concatenate_python_files(folder_to_scan)