Untitled
unknown
python
a year ago
1.1 kB
9
Indexable
import os import json # Set the path to the folder containing your text files folder_path = "/path/to/your/folder" # Create an empty list to store the objects data = [] # Iterate over the files in the folder for filename in os.listdir(folder_path): if filename.endswith(".txt"): file_path = os.path.join(folder_path, filename) # Read the content of the text file with open(file_path, 'r', encoding='utf-8') as file: content = file.read() # Create the object and append it to the list data.append({ "prompt": "", # The prompt is always empty "response": content # The response contains the context of the text file }) # Define the path for the JSON file where the objects will be stored json_file_path = "/path/to/output/data.json" # Write the data to the JSON file with open(json_file_path, 'w', encoding='utf-8') as json_file: json.dump(data, json_file, indent=4) print(f"Data from {len(data)} text files has been stored in {json_file_path}")
Editor is loading...