-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombiner.py
70 lines (55 loc) · 2.2 KB
/
Combiner.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Project Description:
Name: Combiner.py
Description: This script is used to automatically extract all text files within a project and consolidate their contents into `output.txt`.
Author: Huoyuuu
Date: 23.7.14
Usage:
Place the Combiner.py file in the project's root directory and execute `python Combiner.py` in the command line. The results will be saved in the output.txt file.
Output.txt Format:
1. Directory tree structure generated by `tree /f`.
--------
2. Name and specific content of File 1
--------
3. Name and specific content of File 2
and so on...
Reference Prompt:
This script consolidates the code of a GitHub project. It starts with the directory structure and presents the specific content of each code file.
"""
import os
# Text processing function
def text_process(filename, input_path):
filename = os.path.join(input_path, filename)
print(filename)
# Exclude the Combiner itself
if filename == os.path.join(os.getcwd(), 'Combiner.py'):
return
with open(filename, 'r', encoding='utf-8') as f:
content = f.read()
with open('output.txt', 'a', encoding='utf-8') as f:
f.write('-------------------' * 2 + '\n')
f.write(os.path.basename(filename) + '\n')
f.write(content + '\n')
f.write('\n')
# Folder processing function: Recursively process subfolders, otherwise call the text processing function
def folder_process(foldername, input_path):
print(os.path.join(input_path, foldername))
for filename in os.listdir(foldername):
filename = os.path.join(input_path, filename)
if os.path.isdir(filename):
folder_process(filename, input_path + '\\' +
os.path.basename(filename))
else:
# Process text files normally
# Non-text files will raise a UnicodeDecodeError, which is handled using the except block
try:
text_process(filename, input_path)
except UnicodeDecodeError:
pass
def main():
content = os.popen('tree /f').read()
with open('output.txt', 'w', encoding='utf-8') as f:
f.write(content + "\n\n")
folder_process(os.getcwd(), os.getcwd())
if __name__ == "__main__":
main()