Safely make I/O operations to files in Python even from multiple threads... and more!
Project description
safeIO
Safely make I/O operations to files in Python even from multiple threads... and more!
Table of Content
What is it?
It's a module which lets you manage your files (most of the time, Input/Output operations) without worrying about accessing the file from two simultaneously.
Some functions may help you managing your files more easily as they are intuitive and things like substractions (TextFile object - TextFile Object returns the Cosine Similarity of the two files), equality (Object == Object), iteration (for line in TextFile object), the rename/move/delete methods are made easier!.
Installation
From PIP
pip install safeIO --upgrade
Objects
TextFile(filepath, encoding="utf-8", blocking=True)
A Text File object
isfile
isfile(callback=None)
Wether the file exists on the disk or not
delete
delete(callback=None)
Deletes the file
rename
rename(newName,overwrite=False,callback=None)
Renames the file and returns its new path
move
move(newPath,overwrite=False,callback=None)
Moves the file and returns its new path
name
name(callback=None)
Returns the file name
fileno
fileno(callback=None)
Returns the file descriptor (int) used by Python to request I/O operations from the operating system.
read
read(position=0,callback=None)
Reads the entire file and returns its content
write
write(data,position=0,callback=None)
Writes (or overwrites) to the file and returns the number of characters written
append
append(data,callback=None)
Appends to the file and returns the number of characters written
readline
readline(position=0,callback=None)
Returns the line of the current position (from the position to the linebreak)
readlines
readlines(position=0,callback=None)
Reads the whole file and returns the lines (separated by a line break)
writelines
writelines(data,position=0,callback=None)
Writes (or overwrites) the given list of lines to the file
appendlines
appendlines(data,callback=None)
Appends the given list of lines to the file
detach
detach(mode="r",callback=None)
Returns the opened IO (TextIOWrapper)
Warning: Make sure to close the file correctly after using the file with detach
BinaryFile(filepath, blocking=True)
A Binary File object
isfile
isfile(callback=None)
Wether the file exists on the disk or not
delete
delete(callback=None)
Deletes the file
rename
rename(newName,overwrite=False,callback=None)
Renames the file and returns its new path
move
move(newPath,overwrite=False,callback=None)
Moves the file and returns its new path
name
name(callback=None)
Returns the file name
fileno
fileno(callback=None)
Returns the file descriptor (int) used by Python to request I/O operations from the operating system.
read
read(position=0,callback=None)
Reads the entire file and returns its content
write
write(data,position=0,callback=None)
Writes (or overwrites) to the file and returns the number of bytes written
append
append(data,callback=None)
Appends to the file and returns the number of bytes written
readline
readline(position=0,callback=None)
Returns the line of the current position (from the position to the linebreak)
readlines
readlines(position=0,callback=None)
Reads the whole file and returns the lines (separated by a line break)
writelines
writelines(data,position=0,callback=None)
Writes (or overwrites) the given list of lines to the file
appendlines
appendlines(data,callback=None)
Appends the given list of lines to the file
detach
detach(mode="rb",callback=None)
Returns the opened IO (TextIOWrapper)
Tips: Make sure to include the "b" access mode in the mode\n
Warning: Make sure to close the file correctly after using the file with detach
JSONFile(filepath, ensure_ascii=False, minify=False, indent=4, separators=(', ', ': '), encoding="utf-8", blocking=True)
A JSON File object
isfile
isfile(callback=None)
Wether the file exists on the disk or not
delete
delete(callback=None)
Deletes the file
rename
rename(newName,overwrite=False,callback=None)
Renames the file and returns its new path
move
move(newPath,overwrite=False,callback=None)
Moves the file and returns its new path
name
name(callback=None)
Returns the file name
fileno
fileno(callback=None)
Returns the file descriptor (int) used by Python to request I/O operations from the operating system.
read
read(position=0,callback=None)
Reads the entire file and returns its content
write
write(data,position=0,callback=None)
Writes (or overwrites) to the file and returns the number of characters written
append
append(data,callback=None)
Appends to the file and returns the number of characters written
detach
detach(mode="r",callback=None)
Returns the opened IO (TextIOWrapper)
Warning: Make sure to close the file correctly after using the file with detach
Tips
- You can temporarily make the operations blocking with the "with" statement like so:
from safeIO import TextFile
f = TextFile("example.txt", blocking=False)
print(f.read()) # prints "None"
with f:
print(f.read()) # prints the content of example.txt
with f as reading_file:
print(reading_file.read()) # prints the content of example.txt
- Try to define the safeIO object at the top of your script and use the same object for all of the operations to the file with:
from safeIO import JSONFile
data_file = JSONFile("data.json", minify=True, blocking=False)
# do a bunch of stuff
data_file.write({"type": "new_data"})
# do more stuff
with data_file as data:
new = data.read()
new["type"] = "new!"
data_file.write(new)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file safeIO-1.2.tar.gz
.
File metadata
- Download URL: safeIO-1.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d480a6dab01a390ebc24c12d6b774ad00cef3db5348ad07d8bd11d272a808cd3 |
|
MD5 | a08b29da46c990f02a5242d6c70d1dca |
|
BLAKE2b-256 | 232f00e4f435cc49f1b600807356e17ec7625f011a5ae86d8b893ab8ddb5bd98 |