File size: 2,521 Bytes
16bfc87 |
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 |
import os
def collect_files(directory, extensions):
collected_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if any(file.endswith(ext) for ext in extensions):
file_path = os.path.join(root, file)
collected_files.append((file_path, file))
return collected_files
def write_combined_file(text_files, media_files, output_file):
with open(output_file, 'w', encoding='utf-8') as aio_file:
# Write text file contents
if text_files:
aio_file.write("# Text Files Contents\n")
for file_path, file_name in text_files:
aio_file.write(f"# File: {file_path}\n")
try:
with open(file_path, 'r', encoding='utf-8') as file:
aio_file.write(file.read())
except Exception as e:
aio_file.write(f"Error reading file {file_path}: {str(e)}\n")
aio_file.write("\n" + "="*80 + "\n\n")
# Write media file paths with folder structure
if media_files:
aio_file.write("# Media File Paths\n")
current_folder = None
for file_path, file_name in sorted(media_files, key=lambda x: x[0]):
folder = os.path.dirname(file_path)
if folder != current_folder:
if current_folder is not None:
aio_file.write("\n" + "="*80 + "\n\n")
aio_file.write(f"# Folder: {folder}\n")
current_folder = folder
aio_file.write(f"File: {file_path}\n")
if current_folder is not None:
aio_file.write("\n" + "="*80 + "\n\n")
def main():
directory = os.getcwd() # Current working directory
# Define extensions for text files and media files
text_extensions = ['.py', '.js', '.html', '.css', '.txt', '.md', '.json']
media_extensions = ['.jpg', '.jpeg', '.png', '.webp', '.avi', '.mp3', '.mp4']
# Collect text files
text_files = collect_files(directory, text_extensions)
# Collect media files
media_files = collect_files(directory, media_extensions)
# Combine both text file contents and media file paths into one file
output_file = 'combined.txt'
write_combined_file(text_files, media_files, output_file)
print(f"All text file contents and media file paths have been written to {output_file}")
if __name__ == "__main__":
main() |