-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathatexit_temp_file_manager_github.py
More file actions
43 lines (34 loc) · 1.48 KB
/
atexit_temp_file_manager_github.py
File metadata and controls
43 lines (34 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import atexit
from pathlib import Path
import os
class TempFileManager:
"""Automatically creates and deletes a temporary file in the script directory using atexit."""
def __init__(self):
# Get the directory where this script is located
self.base_dir = Path(__file__).parent.resolve()
self.file_path = self.base_dir / "temp_session.txt"
# Create the file
with open(self.file_path, "w", encoding="utf-8") as f:
f.write("=== Temporary Session File ===\n")
print(f"-- Temporary file created at: {self.file_path}")
# Register cleanup function to delete the file when program exits
atexit.register(self.cleanup)
def write_data(self, data: str):
"""Write data into the temporary file."""
with open(self.file_path, "a", encoding="utf-8") as f:
f.write(f"{data}\n")
print(f"- Data written: {data}")
def cleanup(self):
"""Delete the temporary file automatically on exit."""
if self.file_path.exists():
os.remove(self.file_path)
print(f"- Temporary file deleted automatically: {self.file_path}")
else:
print("⚠️ File already deleted or missing.")
def main():
manager = TempFileManager()
manager.write_data("Running analysis...")
manager.write_data("Logging progress...")
print("Program finished. File will be deleted on exit.")
if __name__ == "__main__":
main()