sciris.sc_fileio

Functions for reading/writing to files, including pickles, JSONs, and Excel.

Highlights:

Functions

dumpstr

Dump an object as a bytes-like string (rarely used); see loadstr()

getfilelist

A shortcut for using glob.

getfilepaths

Alias for getfilelist() that returns paths by default instead of strings.

ispath

Alias to isinstance(obj, Path).

jsonify

This is the main conversion function for Python data-structures into JSON-compatible data structures (note: sanitizejson()/jsonify() are identical).

jsonpickle

Save any Python object to a JSON file using jsonpickle.

jsonunpickle

Use jsonpickle to restore an object (see jsonpickle())

load

Load a file that has been saved as a gzipped pickle file, e.g.

loadjson

Convenience function for reading a JSON file (or string).

loadobj

Load a file that has been saved as a gzipped pickle file, e.g.

loadspreadsheet

Load a spreadsheet as a dataframe or a list of lists.

loadstr

Like load(), but for a bytes-like string (rarely used).

loadtext

Convenience function for reading a text file

loadyaml

Convenience function for reading a YAML file (or string).

loadzip

Convenience function for reading a zip file

makefailed

Create a class -- not an object! -- that contains the failure info for a pickle that failed to load

makefilepath

Utility for taking a filename and folder -- or not -- and generating a valid path from them.

makepath

Alias for makefilepath() that returns a path by default instead of a string (with apologies for the confusing terminology, kept for backwards compatibility).

path

Alias to pathlib.Path() with some additional input sanitization:

rmpath

Remove file(s) and folder(s).

sanitizefilename

Takes a potentially Linux- and Windows-unfriendly candidate file name, and returns a "sanitized" version that is more usable.

sanitizejson

This is the main conversion function for Python data-structures into JSON-compatible data structures (note: sanitizejson()/jsonify() are identical).

sanitizepath

Alias for sanitizefilename() that returns a path by default instead of a string.

save

Save an object to file as a gzipped pickle -- use compression 5 by default, since more is much slower but not much smaller.

savejson

Convenience function for saving to a JSON file.

saveobj

Save an object to file as a gzipped pickle -- use compression 5 by default, since more is much slower but not much smaller.

savespreadsheet

Semi-simple function to save data nicely to Excel.

savetext

Convenience function for saving a text file -- accepts a string or list of strings; can also save an arbitrary object, in which case it will first convert to a string.

saveyaml

Convenience function for saving to a YAML file.

savezip

Create a zip file from the supplied list of files (or less commonly, supplied data)

thisdir

Tiny helper function to get the folder for a file, usually the current file.

thisfile

Return the full path of the current file.

thispath

Alias for thisdir() that returns a path by default instead of a string.

zsave

Save a file using zstandard (instead of gzip) compression.

Classes

Blobject

A wrapper for a binary file -- rarely used directly.

Empty

Another empty class to represent a failed object loading, but do not proceed with setstate

Failed

An empty class to represent a failed object loading

Spreadsheet

A class for reading and writing Excel files in binary format.

UniversalFailed

A universal failed object class, that preserves as much data as possible