Package: safeIO
safeIOSafely make I/O operations to files in Python even from multiple threads... and more!Table of ContentsafeIOTable of ContentWhat is it?InstallationObjectsTextFile()BinaryFile()JSONFile()TipsWhat 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!.InstallationFrom PIPpipinstallsafeIO--upgradeObjectsTextFile(filepath, encoding="utf-8", blocking=True)A Text File objectisfileisfile(callback=None)Wether the file exists on the disk or notdeletedelete(callback=None)Deletes the filerenamerename(newName,overwrite=False,callback=None)Renames the file and returns its new pathmovemove(newPath,overwrite=False,callback=None)Moves the file and returns its new pathnamename(callback=None)Returns the file namefilenofileno(callback=None)Returns the file descriptor (int) used by Python to request I/O operations from the operating system.readread(position=0,callback=None)Reads the entire file and returns its contentwritewrite(data,position=0,callback=None)Writes (or overwrites) to the file and returns the number of characters writtenappendappend(data,callback=None)Appends to the file and returns the number of characters writtenreadlinereadline(position=0,callback=None)Returns the line of the current position (from the position to the linebreak)readlinesreadlines(position=0,callback=None)Reads the whole file and returns the lines (separated by a line break)writelineswritelines(data,position=0,callback=None)Writes (or overwrites) the given list of lines to the fileappendlinesappendlines(data,callback=None)Appends the given list of lines to the filedetachdetach(mode="r",callback=None)Returns the opened IO (TextIOWrapper)Warning: Make sure to close the file correctly after using the file with detachBinaryFile(filepath, blocking=True)A Binary File objectisfileisfile(callback=None)Wether the file exists on the disk or notdeletedelete(callback=None)Deletes the filerenamerename(newName,overwrite=False,callback=None)Renames the file and returns its new pathmovemove(newPath,overwrite=False,callback=None)Moves the file and returns its new pathnamename(callback=None)Returns the file namefilenofileno(callback=None)Returns the file descriptor (int) used by Python to request I/O operations from the operating system.readread(position=0,callback=None)Reads the entire file and returns its contentwritewrite(data,position=0,callback=None)Writes (or overwrites) to the file and returns the number of bytes writtenappendappend(data,callback=None)Appends to the file and returns the number of bytes writtenreadlinereadline(position=0,callback=None)Returns the line of the current position (from the position to the linebreak)readlinesreadlines(position=0,callback=None)Reads the whole file and returns the lines (separated by a line break)writelineswritelines(data,position=0,callback=None)Writes (or overwrites) the given list of lines to the fileappendlinesappendlines(data,callback=None)Appends the given list of lines to the filedetachdetach(mode="rb",callback=None)Returns the opened IO (TextIOWrapper)Tips: Make sure to include the "b" access mode in the mode\nWarning: Make sure to close the file correctly after using the file with detachJSONFile(filepath, ensure_ascii=False, minify=False, indent=4, separators=(', ', ': '), encoding="utf-8", blocking=True)A JSON File objectisfileisfile(callback=None)Wether the file exists on the disk or notdeletedelete(callback=None)Deletes the filerenamerename(newName,overwrite=False,callback=None)Renames the file and returns its new pathmovemove(newPath,overwrite=False,callback=None)Moves the file and returns its new pathnamename(callback=None)Returns the file namefilenofileno(callback=None)Returns the file descriptor (int) used by Python to request I/O operations from the operating system.readread(position=0,callback=None)Reads the entire file and returns its contentwritewrite(data,position=0,callback=None)Writes (or overwrites) to the file and returns the number of characters writtenappendappend(data,callback=None)Appends to the file and returns the number of characters writtendetachdetach(mode="r",callback=None)Returns the opened IO (TextIOWrapper)Warning: Make sure to close the file correctly after using the file with detachTipsYou can temporarily make the operations blocking with the "with" statement like so:fromsafeIOimportTextFilef=TextFile("example.txt",blocking=False)print(f.read())# prints "None"withf:print(f.read())# prints the content of example.txtwithfasreading_file:print(reading_file.read())# prints the content of example.txtTry 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:fromsafeIOimportJSONFiledata_file=JSONFile("data.json",minify=True,blocking=False)# do a bunch of stuffdata_file.write({"type":"new_data"})# do more stuffwithdata_fileasdata:new=data.read()new["type"]="new!"data_file.write(new)