import os # Directory containing the .txt files directory = '.' # Name of the output file output_file = 'merged_content.txt' # Open the output file in write mode with open(output_file, 'w', encoding='utf-8') as outfile: # Iterate over all files in the directory for filename in os.listdir(directory): # Check if the file starts with 'content_' and ends with '.txt' if filename.startswith('content_') and filename.endswith('.txt'): with open(os.path.join(directory, filename), 'r', encoding='utf-8') as infile: # Write the content of the file to the output file outfile.write(infile.read().replace('*','')) outfile.write('\n') # Add a newline after each file's content # Convert the merged text file to .mobi format using Calibre's ebook-convert os.system('ebook-convert merged_content.txt merged_content.mobi')