Write content to a file. Args: path(`path`): The relative file path to write into, a prefix dir will be automatically concatenated. content: Returns: or error message.
(self, path: str, content: str)
| 450 | e) |
| 451 | |
| 452 | async def write_file(self, path: str, content: str): |
| 453 | """Write content to a file. |
| 454 | |
| 455 | Args: |
| 456 | path(`path`): The relative file path to write into, a prefix dir will be automatically concatenated. |
| 457 | content: |
| 458 | |
| 459 | Returns: |
| 460 | <OK> or error message. |
| 461 | """ |
| 462 | try: |
| 463 | if not os.path.exists(self.output_dir): |
| 464 | os.makedirs(self.output_dir, exist_ok=True) |
| 465 | original_path = path # Preserve original path for error messages |
| 466 | path = self.get_real_path(path) |
| 467 | if path is None: |
| 468 | return f'<{original_path}> is out of the valid project path: {self.output_dir}' |
| 469 | dirname = os.path.dirname(path) |
| 470 | if dirname: |
| 471 | os.makedirs( |
| 472 | os.path.join(self.output_dir, dirname), exist_ok=True) |
| 473 | with open(os.path.join(self.output_dir, path), 'w') as f: |
| 474 | f.write(content) |
| 475 | return f'Save file <{path}> successfully.' |
| 476 | except Exception as e: |
| 477 | return f'Write file <{path}> failed, error: ' + str(e) |
| 478 | |
| 479 | async def replace_file_contents(self, |
| 480 | path: str, |
no test coverage detected