hbutils.file.stream¶
- Overview:
Utilities for processing streams.
keep_cursor¶
-
hbutils.file.stream.
keep_cursor
(file: Union[TextIO, BinaryIO]) → AbstractContextManager[source]¶ - Overview:
Keep the cursor of the given file within a with-block.
- Parameters:
file – File which cursor need to be kept.
- Examples::
>>> import io >>> from hbutils.file import keep_cursor >>> >>> with io.BytesIO(b'\xde\xad\xbe\xef') as file: ... with keep_cursor(file): ... print(file.read(2)) ... with keep_cursor(file): # still from 0 ... print(file.read()) ... ... _ = file.read(2) ... with keep_cursor(file): # now from 2 ... print(file.read(1)) ... with keep_cursor(file): # still from 2 ... print(file.read()) b'\xde\xad' b'\xde\xad\xbe\xef' b'\xbe' b'\xbe\xef'
Note
Only seekable stream can use
keep_cursor()
.getsize¶
-
hbutils.file.stream.
getsize
(file: Union[TextIO, BinaryIO]) → int[source]¶ - Overview:
Get the size of the given
file
stream.
- Parameters:
file – File which size need to access.
- Returns:
File’s size.
- Examples::
>>> import io >>> from hbutils.file import getsize >>> >>> with io.BytesIO(b'\xde\xad\xbe\xef') as file: ... print(getsize(file)) 4 >>> with open('README.md', 'r') as file: ... print(getsize(file)) 2582
Note
Only seekable stream can use
getsize()
.is_eof¶
-
hbutils.file.stream.
is_eof
(file: Union[TextIO, BinaryIO]) → bool[source]¶ - Overview:
Check if the file meets its end.
- Parameters:
file – File to be checked.
- Returns:
Is EOF(end of file) or not.
- Examples::
>>> import io >>> from hbutils.file import is_eof >>> >>> with io.BytesIO(b'\xde\xad\xbe\xef') as file: ... print(file.tell(), is_eof(file)) ... _ = file.read(2) ... print(file.tell(), is_eof(file)) ... _ = file.read(2) ... print(file.tell(), is_eof(file)) 0 False 2 False 4 True >>> with open('README.md', 'r') as file: ... print(file.tell(), is_eof(file)) ... _ = file.read(100) ... print(file.tell(), is_eof(file)) ... _ = file.read() ... print(file.tell(), is_eof(file)) 0 False 100 False 2582 True
Note
Only seekable stream can use
is_eof()
.