Skip to content

Instantly share code, notes, and snippets.

@esmitt
Created September 25, 2024 16:53
Show Gist options
  • Save esmitt/15def78c453113103c363c718aabacf1 to your computer and use it in GitHub Desktop.
Save esmitt/15def78c453113103c363c718aabacf1 to your computer and use it in GitHub Desktop.
Return a single string with the content of all files inside a folder. For each file, it indicates the filename and its content. for now, ignore single-line comments. Very useful to ask to ChatGPT for instance about what is wrong with your code
from pathlib import PurePath, Path
def stringify_file(filename: str) -> str:
single_file = ""
with open(filename) as file:
for line in file:
if len(line.strip()) > 0:
line = line.split('#')
if len(line) == 1: # the line has comments, then ignore them
single_file += line[0]
return single_file
def single_str_for_python_files(directory: PurePath) -> str:
result: str = ""
for filename in Path(directory).rglob("*.py"):
if filename.name != "__init__.py":
result += f"{filename.relative_to(filename.parent.parent)}:\n"
result += stringify_file(str(filename.absolute()))
return result
print(single_str_for_python_files(Path.cwd() / "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment