This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseek_file.py
More file actions
65 lines (50 loc) · 1.88 KB
/
seek_file.py
File metadata and controls
65 lines (50 loc) · 1.88 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
from pathlib import Path
from typing import AnyStr
class SeekFile:
def __init__(self, file_path: Path | str, mode: str = 'rb+'):
"""Initialize the SeekFile object and open the file."""
if type(file_path) == str:
file_path = Path(file_path)
self.file_path = file_path
self.touch()
self.mode = mode
self.file = None
self.open()
def touch(self):
self.file_path.touch(exist_ok = True)
def open(self):
self.touch()
self.file = open(self.file_path, self.mode)
def seek(self, offset: int, whence: int = 0):
"""Move the file pointer to the specified offset. By default, from the beginning."""
self.file.seek(offset, whence)
def seek_to_end(self):
"""Move the file pointer to the end of the file."""
self.seek(0, os.SEEK_END)
def read(self, num_bytes: int) -> AnyStr:
"""Read a specified number of bytes from the current file pointer position."""
return self.file.read(num_bytes)
def read_all_from_offset(self, offset: int) -> AnyStr:
"""Read all data from the specified offset to the end of the file."""
self.seek(offset)
return self.file.read()
def write(self, data: AnyStr):
"""Write data at the current file pointer position."""
self.file.write(data)
def write_at_offset(self, offset: int, data: AnyStr):
"""Write data at a specified offset."""
self.seek(offset)
self.write(data)
def get_position(self) -> int:
"""Get current position"""
return self.file.tell()
def close(self):
"""Close the file."""
self.file.close()
def delete(self):
self.file_path.unlink(missing_ok = True)
self.close()
def __del__(self):
"""Ensure the file is closed when the object is deleted."""
self.close()