chapter 2.4

This commit is contained in:
2025-08-05 14:48:45 +02:00
parent 5f32644822
commit 495935dc74
6 changed files with 25 additions and 74 deletions

17
functions/write_file.py Normal file
View File

@@ -0,0 +1,17 @@
import os
def write_file(working_directory, file_path, content):
abs_working_dir = os.path.abspath(working_directory)
abs_file_path = os.path.abspath(os.path.join(working_directory, file_path))
if not abs_file_path.startswith(abs_working_dir):
return f'Error: Cannot write to "{file_path}" as it is outside the permitted working directory'
if not os.path.exists(abs_file_path):
os.makedirs(abs_file_path)
try:
if os.path.exists(abs_file_path):
with open(abs_file_path, 'w') as f:
f.write(content)
return f'Successfully wrote to "{file_path}" ({len(content)} characters written)'
except Exception as e:
print(f"Error: {e}")