INNER CODE UNIT · Python
combine_chapters
DrewThomasson/VoxNovel · 1CPU_Book_processing.py:292
def combine_chapters(input_folder, output_file):
# Create the output folder if it doesn't exist
os.makedirs(os.path.dirname(output_file), exist_ok=True)
# List all txt files and sort them by chapter number
files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
sorted_files = sorted(files, key=sort_key)
with open(output_file, 'w') as outfile:
for i, filename in enumerate(sorted_files):
with open(os.path.join(input_folder, filename), 'r') as infile:
outfile.write(infile.read())
# Add the marker unless it's the last file
if i < len(sorted_files) - 1:
outfile.write("\nNEWCHAPTERABC\n")
# Paths
input_folder = 'Working_files/temp_ebook'