sc_fileio#

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

Highlights:

Classes

Blobject

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

Spreadsheet

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

Failed

An empty class to represent a failed object loading.

Functions

load

Load a file that has been saved as a gzipped pickle file, e.g. by sc.save().

save

Save any object to disk

loadobj

Load a file that has been saved as a gzipped pickle file, e.g. by sc.save().

saveobj

Save any object to disk

zsave

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

loadstr

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

dumpstr

Dump an object to a bytes-like string (rarely used by the user); see sc.save() instead.

loadtext

Convenience function for reading a text file

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.

loadzip

Load the contents of a zip file into a variable

unzip

Convenience function for reading a zip file

savezip

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

path

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

ispath

Alias to isinstance(obj, Path).

thisfile

Return the full path of the current file.

thisdir

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

thispath

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

getfilelist

A shortcut for using glob.glob().

glob

A shortcut for using glob.glob().

getfilepaths

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

sanitizefilename

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

sanitizepath

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

makefilepath

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

makepath

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

rmpath

Remove file(s) and folder(s).

loadany

Load data from a file using all known load functions until one works.

sanitizejson

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

jsonify

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

printjson

Print an object as a JSON

readjson

Read JSON from a string

loadjson

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

savejson

Convenience function for saving to a JSON file.

readyaml

Read YAML from a string

loadyaml

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

saveyaml

Convenience function for saving to a YAML file.

jsonpickle

Save any Python object to a JSON using jsonpickle.

jsonunpickle

Open a saved JSON pickle

loadspreadsheet

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

savespreadsheet

Semi-simple function to save data nicely to Excel.

Exceptions

UnpicklingWarning

A warning raised when unpickling an object fails

UnpicklingError

An error raised when unpickling an object fails