backpack.filemanip

Useful functions for everyday use —> files and folders

brixs.backpack.filemanip.rename_files(filelist, pattern, new_pattern, ask=True)

Change the filename pattern of files.

Parameters:
  • filelist (list) – list of filepaths (string or pathlib.Path object).

  • pattern (str) – string that represents the filename pattern. Use {} to collect values within the file name.

  • new_pattern (str) –

    Each chunk of information assigned with {} in pattern is assigned a number based on its position. The first chunk is 0, then 1, and so on. Use {position} to define the new pattern,

    Example

    If filename is ‘data_5K-8.dat’, pattern = {}_{}K-{}.dat and new_pattern={2}_{1}K.dat. The new filename will be: ‘8_5K.dat’.

    Note

    Use \ as an escape key.

    Note

    Use {n} in new_pattern to inlcude the filenane index number (index regarding filelist).

  • ask (bool) – If true, it shows all the new filenames and asks for permission to rename.

brixs.backpack.filemanip.rmdir(dirpath, not_found_ok=False)

Remove a directory and everyting in it.

Parameters:

dirpath (string or pathlib.Path) – directory path.

brixs.backpack.filemanip.rm(filepath)

Remove a file.

Parameters:

filepath (string or pathlib.Path) – filepath.

brixs.backpack.filemanip.filelist(dirpath='.', string='*', case_sensitive=True)

Returns a list with all the files containing string in its name.

Note

List is sorted by the filename.

Parameters:
  • dirpath (str or pathlib.Path, optional) – list with full file directory paths.

  • string (str, optional) – pattern. only filenames with this string will be considered. Use ‘*’ for matching anything. Default is ‘*’.

  • case_sensitive (bool or None, optional) – Default is True. If None, case_sensitive will matches paths using platform-specific casing rules. Typically, case-sensitive on POSIX, and case-insensitive on Windows.

Returns:

list

brixs.backpack.filemanip.parsed_filelist(dirpath='.', string='*', ref=0, _type='int', return_type='list')

Returns a filelist organized in a dictionary.

I searches for numbers (float and int) within the filenames (or foldernames) and uses them as keys for the dictionary.

Parameters:
  • dirpath (str or pathlib.Path, optional) – directory path.

  • string (str, optional) – string to filter filenames.

  • ref (int, optional) – index of the reference number to be used as key.

  • _type (string, optional) – if ‘int’, dict keys are transformed in integers. If ‘float’, dict keys are transformed into float.

Returns:

if return type is ‘list’

returns a list

if return type is ‘dict’

ordered Dictionary. Dict keys are some number found in filename.

See also

filelist()

brixs.backpack.filemanip.get_modified_date(filepath)

Return modified date of filepath in a datetime object.

brixs.backpack.filemanip.save_text(string, filepath='./Untitled.txt', check_overwrite=False, **kwargs)

Save text to txt file.

Parameters:
  • string (str) – string to be saved.

  • filepath (str or pathlib.Path, optional) – path to save file. If no path is given, current working directory is used.

  • check_overwrite (bool, optional) – if True, it will check if file exists and ask if user want to overwrite file.

  • open (**kwargs are passed to)

Return type:

None

See also

load_text()

brixs.backpack.filemanip.load_text(filepath)

Load text from txt file.

Parameters:

filepath (str or pathlib.Path) – filepath to load.

Returns:

string.

See also

save_text()

brixs.backpack.filemanip.save_obj(obj, filepath='./Untitled.txt', check_overwrite=False, pretty_print=True)

Save object (array, dictionary, list, etc…) to a txt file.

Parameters:
  • obj (object) – object to be saved.

  • filepath (str or pathlib.Path, optional) – path to save file.

  • check_overwrite (bool, optional) – if True, it will check if file exists and ask if user want to overwrite file.

Return type:

None

See also

load_obj()

brixs.backpack.filemanip.load_obj(filepath, dict_keys_to_int=False)

Load object (array, dictionary, list, etc…) from a txt file.

Parameters:
  • filepath (str or pathlib.Path) – file path to load.

  • dict_keys_to_int (bool, optional) – If True, it will change ALL numeric dict keys (even for key in nested dictionarys to int, e.g., dictObject[“0.0”] will turn into dictObject[0].

Returns:

object.

See also

save_obj()

brixs.backpack.filemanip.load_Comments(filepath, comment_flag='#', stop_flag='#')

Return comments from text files.

Comments must be indicated at the beginning of the line by the comment flag.

Parameters:
  • filepath (str or pathlib.Path) – fullpath to file

  • comment_flag (str, optional) – string that indicate line is a comment.

  • stop_flag (str, optional) – string that indicates line to stop looking for comments, e.g. #f, or #L. Use None to read all lines in file (useful for reading file with comments not only at the beginning). If stop_flag is equal to comment_flag it will read from the first line with comment_flag and keep reading until comment_flag does not appear anymore (useful to read comments at the beginning of a file).

Returns:

list with comments or False if no comment is found.

brixs.backpack.filemanip.save_data(obj, filepath='./untitled.txt', add_labels=True, fmt='% .10e', header='', footer='', delimiter=', ', comment_flag='# ', newline='\n', check_overwrite=False)

Save an array or a dictionary in a txt file.

Parameters:
  • obj (dict, list, or numpy.array) – data to be saved to a file. If obj is

  • dictionary (a)

  • file. (use * in front of a key to do not save it to the)

  • filepath (str or pathlib.Path, optional) – path to save file.

  • add_labels (bool, optional) – When obj is a dictionary, add_labels=True makes the dict keys to be added to the header as label for each data column.

  • fmt (string, or list, optional) –

    format for saving data. If string, the value is used for x- and y-coordinates. If tuple of strings, the first string is used for x-coordinates and the second for y-coordinates.

    fmt = (%[flag]width[.precision]specifier)

    • flag can be:

      1. ’-’ for left justify

      2. ’+’, puts + or - in front of the numbers

      3. ’0’ to Left pad the number with zeros instead of space (see width).

    • width is the minimum number of characters to be printed.

    • precision is the number of significant digits.

    • specifier is the type of notation. Typically, either ‘e’ for scientific notation of ‘f’ for decimal floating point.

    • a common fmt strings is: ‘%.3f’ for 3 decimal places.

    • for more information see np.savetxt documentation:

  • header (str, optional) – string that will be written at the beginning of the file (comment flag is added automatically).

  • footer (str, optional) – string that will be written at the end of the file (comment flag is added automatically).

  • delimiter (str, optional) – The string used to separate values.

  • comment_flag (str, optional) – string that flag comments.

  • newline (str, optional) – string to indicate new lines.

  • check_overwrite (bool, optional) – if True, it will check if file exists and ask if user want to overwrite file.

Return type:

None

See also

load_data()

brixs.backpack.filemanip.load_data(filepath, labels=None, force_array=False, **kwargs)

Load data from text file. Wrapper for np.genfromtxt.

Data is formatted in a dictionary or array.

The dictionary keys are set as the label of the corresponding data columns, where the last comment line before data starts is assumed to have the labels of each data column. If column labels cannot be found, data is imported as an array. The file expects comments at the beginning of the file (file must not have comments elsewhere).

If a column is full of NaN’s, it’s converted to a column full of zeros.

Parameters:
  • filepath (str or pathlib.Path) – path to file

  • labels (list, optional) – It forces data to be loaded as a dictionary where each label is associated with a data column. Its length must have the same as the number of columns. To avoid importing a column, put an asterisk (*) in front of the corresponding label.

  • force_array (bool, optional) – If force_array=True, data it will be returned in a array. Overwrites labels.

If not specified, the following parameters are passed to np.savetxt:

Parameters:
  • delimiter (str, optional) – String or character separating columns. Use \t for tab. If None, the script will read the file and try to guess the appropriate delimiter. If the file has a header, and if the header has a line with the column labels, it tries to guess the delimiter of this line. If it cannot, it tries to use the same delimiter for the data and for the leader in the header. Default is None.

  • comments (str, optional) – The character used to indicate the start of a comment. Default is ‘#’.

  • skip_header (int, optional) – The number of lines to skip at the beginning of the file. Default is 0.

Returns:

Dictionary or array.

See also

save_data().