Skip to content

[BUG]Console.save_text clears recorded output when file write fails #4208

Description

@sanjay3941
  • I've checked the docs and closed issues for possible solutions.
  • I can't find my issue in the FAQ.

Describe the bug

When using Console(record=True), calling save_text() with clear=True
appears to clear the recorded console output even when the file cannot be
successfully written.

Minimal Repoduction of the issue:

from rich.console import Console

console = Console(record=True)

console.print("IMPORTANT OUTPUT")

print("Before failed save:")
print(repr(console.export_text()))

try:
    console.save_text(
        r"Z:\this\path\should\not\exist\output.txt"
    )
except Exception as e:
    print("\nSave failed:")
    print(type(e).__name__, e)

print("\nAfter failed save:")
print(repr(console.export_text()))

The output:
Before failed save:
'IMPORTANT OUTPUT\n'

Save failed:
FileNotFoundError [Errno 2] No such file or directory: 'Z:\this\path\should\not\exist\output.txt'

After failed save:
''

The file was not successfully written, but the record output was also cleared.

i would expect the recorded output to remain available when the save fails since the output was not sucessfully persisted.

but i am not sure whether the current behavior is intentional or not .? if not could it be like the buffer could be preserved until the file write succeeds ?

Platform:
Windows 11
Python 3.13
Powershell and VS code terminal

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions