improved append file
This commit is contained in:
44
file_append.py
Normal file
44
file_append.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
|
||||
|
||||
def check_append_file(prompt: str) -> str:
|
||||
if "@" in prompt:
|
||||
parts = prompt.split(" ")
|
||||
content = []
|
||||
for part in parts:
|
||||
if part.startswith("@"):
|
||||
filename = part[1:]
|
||||
try:
|
||||
if os.path.exists(filename):
|
||||
with open(filename, "r") as f:
|
||||
content.append("%s:'''\n%s'''" % (filename, f.read()))
|
||||
except FileNotFoundError:
|
||||
print(f"File '{filename}' not found.")
|
||||
content.append(prompt)
|
||||
return "\n".join(content)
|
||||
return prompt
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit() # not accidentally trigger it
|
||||
|
||||
# Create some sample files
|
||||
with open("fmain.py", "w") as f:
|
||||
f.write("# This is main.py\n")
|
||||
with open("finference.py", "w") as f:
|
||||
f.write("# This is inference.py\n")
|
||||
|
||||
# Test cases
|
||||
test_prompts = [
|
||||
"@fmain.py",
|
||||
"@fmain.py @finference.py",
|
||||
"@fnonexistent.py",
|
||||
"@fmain.py @fnonexistent.py"
|
||||
]
|
||||
|
||||
for prompt in test_prompts:
|
||||
print(f"Testing prompt: {prompt}")
|
||||
result = check_append_file(prompt)
|
||||
print(f"Result: {result}")
|
||||
print("-" * 20)
|
Reference in New Issue
Block a user