luna.util.file module¶
- create_directory(path, clear=False)[source]¶
Create the directory pathaname
path
.- Parameters
path (str) – The directory pathname to be created.
clear (bool) – If True, clear the directory if already exists. The default value is False.
- detect_compression_format(filename)[source]¶
Attempts to detect compression format from the filename extension. Returns None if no format could be detected.
- generate_json_file(json_data, output_file, indent=4, sort_keys=True)[source]¶
Serialize
json_data
to a JSON formatted string and save it atoutput_file
.- Parameters
json_data (object) – The data to be serialized.
output_file (str) – The output file where the serialized data will be saved.
indent (int) – If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, negative, or “” will only insert newlines. None selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as ” “), that string is used to indent each level. The default value is 4.
sort_keys (bool) – If
sort_keys
is True, the output of dictionaries will be sorted by key.
- generic_splitext(path, max_split=None)[source]¶
Split the pathname
path
into a pair (filename
,ext
).- Parameters
path (str) – The pathname.
max_split (int or None) – Specifies how many splits to do. The default value is None, which is “all occurrences”.
- Returns
filename (str) – The filename.
ext (str) – The file extension.
- get_file_format(path, max_split=None, ignore_compression=False)[source]¶
Detect the file format from pathname
path
.- Parameters
path (str) – The pathname.
max_split (int or None) – Specifies how many splits to do. The default value is None, which is “all occurrences”.
ignore_compression (bool, optional) – Ignore compression format. The default value is False, which does not ignore compression format.
- Returns
filename – The file format.
- Return type
- get_filename(path, max_split=None)[source]¶
Detect the filename from pathname
path
.- Parameters
path (str) – The pathname.
max_split (int or None) – Specifies how many splits to do. The default value is None, which is “all occurrences”.
- Returns
filename – The filename.
- Return type
- new_unique_filename(path, size=32, chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', retries=5)[source]¶
Generate a new unique random pathname.
- Parameters
path (str) – The target pathname.
size (int, optional) – The size of the new filename. The default value is 32.
chars (iterable, optional) – A sequence of characters to choose from. The default value is ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789’.
retries (int, optional) – The function will keep trying to generate a unique filename (not exist in the pathname
path
) until the maximum number of retriesretries
is reached.
- Returns
unique_pathname – A new random unique pathname (path + random filename).
- Return type
- parse_json_file(json_file)[source]¶
Deserialize the JSON file
json_file
.- Parameters
json_file (str) – The input JSON file.
- Returns
json_data – The deserialized data.
- Return type
- pickle_data(data, output_file, compressed=True)[source]¶
Write the pickled representation of the object
data
to the fileoutput_file
.- Parameters
data (object) – The object to be pickled.
output_file (str) – The output file where the pickled representation will be saved.
compressed (bool, optional) – If True (the default), compress the pickled representation as a gzip file (.gz).
- Raises
FileNotCreated – If the file could not be created.
- remove_directory(path, only_empty_paths=False)[source]¶
Remove the directory given by the pathname
path
.- Parameters
path (str) – The pathname.
only_empty_paths (bool, optional) – If True, do not remove non-empty directories. The default value is False, which removes all directories.
- remove_files(files)[source]¶
Remove the provided files.
- Parameters
files (iterable) – An iterable object containing a sequence of files to be removed.
- unpickle_data(input_file)[source]¶
Read the pickled representation of an object from the file
input_file
and return the reconstituted object hierarchy specified therein.input_file
can be a gzip-compressed file.- Raises
PKLNotReadError – If the file could not be loaded.
- validate_filesystem(path, type)[source]¶
Validate if a file or directory exists and if it has the expected type.
- Parameters
path (str) – The pathname of the file or directory to be validated.
type ({‘file’, ‘directory’}) – The filesystem type.
- Raises
FileNotFoundError – If the file or directory given by the pathname
path
does not exist.IsADirectoryError – If a file is provided but a directory is found instead at the pathname
path
.NotADirectoryError – If the filesystem given by the pathname
path
is not a directory.