Created
October 13, 2023 19:23
-
-
Save Mapagmataas1331/86f4cf9f2ad1f9dfd60e85f00ba4bb88 to your computer and use it in GitHub Desktop.
This Python script is a file content extraction tool that lets you input a project directory, gather data from files within that directory, and save their content to one text file. It can handle both individual files and entire directories, and it's designed to gracefully handle non-text files, directories, or missing files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
# Function to read the content of a file and return it as a string | |
def read_file_content(file_path): | |
try: | |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as file: | |
content = file.read() | |
return content | |
except (UnicodeDecodeError, IsADirectoryError, FileNotFoundError): | |
# Handle non-text files or directories, and missing files | |
return "Non-text file, directory, or missing file, content not read" | |
# Function to save file names and their data | |
def save_file_data(project_directory, output_file): | |
file_data_list = [] | |
def process_directory(directory): | |
for root, _, files in os.walk(directory): | |
for file in files: | |
file_path = os.path.join(root, file) | |
relative_path = os.path.relpath(file_path, project_directory) | |
file_content = read_file_content(file_path) | |
file_data_list.append((relative_path, file_content)) | |
while True: | |
# Prompt the user for the path to a file, directory, or 'stop' | |
path = input("Enter the path to a file, directory or write 'this' or 'stop': ") | |
if path.lower() == 'stop': | |
break | |
# If the user enters 'this', use the project directory as the path | |
if path.lower() == 'this': | |
process_directory(project_directory) | |
break | |
else: | |
file_path = os.path.join(project_directory, path) | |
if os.path.isfile(file_path): | |
file_content = read_file_content(file_path) | |
file_data_list.append((path, file_content)) | |
elif os.path.isdir(file_path): | |
process_directory(file_path) | |
else: | |
print(f"File or directory not found: {path}") | |
# Write the file names and their data to a text document | |
with open(output_file, 'w') as file: | |
for file_data in file_data_list: | |
file.write(f"{file_data[0]}:\n{file_data[1]}\n\n") | |
print(f"File names and data saved to '{output_file}'") | |
# Prompt the user for the project directory | |
project_directory = input("Enter the path to the project directory: ") | |
# Specify the output file | |
output_file = 'file_data.txt' | |
# Call the function to save file data | |
save_file_data(project_directory, output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment