Skip to content

Instantly share code, notes, and snippets.

@jlleblanc
Created July 12, 2024 15:50
Show Gist options
  • Save jlleblanc/e6baba564865039258db8f6d4e9bbebd to your computer and use it in GitHub Desktop.
Save jlleblanc/e6baba564865039258db8f6d4e9bbebd to your computer and use it in GitHub Desktop.
Claude’s current preferred way of importing a file structure and contents
import os
def create_file_structure(base_path="."):
file_structure = {}
for root, dirs, files in os.walk(base_path):
relative_path = os.path.relpath(root, base_path)
for dir_name in dirs:
file_structure[os.path.join(relative_path, dir_name)] = {}
for file_name in files:
file_path = os.path.join(root, file_name)
relative_file_path = os.path.join(relative_path, file_name)
try:
with open(file_path, "r", encoding="utf-8") as file:
file_structure[relative_file_path] = file.read()
except UnicodeDecodeError:
try:
with open(file_path, "r", encoding="latin-1") as file:
file_structure[relative_file_path] = file.read()
except UnicodeDecodeError:
print(f"Skipping file: {file_path} (unsupported encoding)")
continue
return file_structure
def generate_documents(file_structure):
documents = ""
for path, content in file_structure.items():
if isinstance(content, dict):
for sub_path, sub_content in content.items():
documents += f"""
<documents>
<document index="{len(documents.split('<document'))}">
<source>{os.path.join(path, sub_path)}</source>
<document_content>
{sub_content}
</document_content>
</document>
</documents>
"""
else:
documents += f"""
<documents>
<document index="{len(documents.split('<document'))}">
<source>{path}</source>
<document_content>
{content}
</document_content>
</document>
</documents>
"""
return documents
def main():
file_structure = create_file_structure()
documents = generate_documents(file_structure)
print(documents)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment