Created
September 25, 2024 16:53
-
-
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
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
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