An IO wrapper that reads/writes binary data as base64 into a text file
Project description
Base64File
An IO wrapper that but reads/writes binary data as base64 text, supporting concurrent reading and writing. Similar
to GzipFile
, it simulates most of the methods of a
standard file object (with the exception of readline
).
- When opening this without specifying an underlying file, a filename must be provided, and the usual binary modes are
supported:
r
,rb
,w
,wb
,a
,ab
,x
, andxb
(optional+
to enable concurrent reading/writing, e.g.wb+
). - When specifying an underlying file, the mode is inherited from the specified file. If a binary-mode file is provided,
it is automatically wrapped with a
TextIOWrapper
.
Usage
base64file.open
import base64file
# to write text
with base64file.open('some-file.txt', 'rt', encoding='utf8') as f:
f.write('1234567890')
# to write text
with base64file.open('some-file.txt', 'rb', encoding='utf8') as f:
f.write(b'\1\2\3\4\5')
base64file.Base64File
from base64file import Base64File
# open a new/existing file for concurrent reading and writing
with Base64File('some-file.txt', 'w+') as b:
b.write(b'\0\1\2\3\4')
b.seek(0)
b.write(b'\n')
print(b.read(2)) # prints b'\1\2'
# wrap an existing file (in this example, open for reading)
with open('some-file.txt', 'r') as f:
with Base64File(file_obj=f) as b:
print(b.read(2)) # prints b'\n\1'
print(b.read()) # prints b'\2\3\4'
# if you need to keep the file open, remember to close it
f = open('some-file.txt', 'w+')
b = Base64File(file_obj=f)
b.write(b'\0\1\2\3\4')
b.seek(0)
b.write(b'\n')
print(b.read(2)) # prints b'\1\2'
b.close() # this is necessary, otherwise the final 1-2 bytes may not be written
f.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
base64file-0.0.7.tar.gz
(9.3 kB
view hashes)
Built Distribution
Close
Hashes for base64file-0.0.7-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0d16c1927cbd5346dcea6d14c02795a31afcb34e5dd99e0a96973aec4f81283 |
|
MD5 | 043ae0ed6879b378311fe37afffda4d6 |
|
BLAKE2b-256 | dd8e2b7d6e631486517206dfb37b5bf816e61a134818f618b10d536a7ec4c4cf |