cmind.utils.file_utils

File Handling Utilities

Functions

cmind_copy_all(source_dir, dest_dir[, ...]) recursively copy (or symlink) all files in source_dir to dest_dir
csv2dict(csvfile[, strip_quotes]) write out a dictionary containing strings or lists of strings into a
dict2csv(input_dict, csvfile) write a simple dictionary into a .csv file containing strings or
export_tarfile(filename, filelist[, ...]) utility for sending a list of files/directories to a .tar archive
file_update_bool(input_file, output_file[, ...]) return a boolean that determines if output_file should be updated based
filecheck_bool(input_file, output_file[, ...]) Returns True if input_file exists, but output_file doesn’t exist
imexist(image_filename[, im_subset]) Extension insensitive image match.
multiglob_list(strlist[, basedir]) concatenate results of multiple glob operations into a single list
remove_file(f[, warn]) simple wrapper to call os.remove() only if the file exists
rm_broken_links([basedir, use_shell]) recursively remove any broken links within basedir via find/exec
split_multiple_ext(input_name) function to recursively remove multiple file extensions from a filename
which(program) Search for a binary program on the system path
cmind.utils.file_utils.cmind_copy_all(source_dir, dest_dir, match_str='*', use_symlinks=False, overwrite_existing=True, prepend_str='', recursive=True)[source]

recursively copy (or symlink) all files in source_dir to dest_dir

Parameters:

source_dir : str

source directory name

dest_dir : str

destination directory name

match_str : str, optional

wildcard string specifying file match

use_symlinks : bool, optional

make symbolic links instead of copies

overwrite_existing : bool, optional

if True, any already existing destination files will be removed before the copy is attempted

prepend_str : str, optional

if specified, prepend this string to all destination filenames

recursive : bool, optional

if False, do not copy from subdirectories of source_dir

Returns:

all_destfile : list of str

list of the files that were copied

cmind.utils.file_utils.csv2dict(csvfile, strip_quotes=False)[source]

write out a dictionary containing strings or lists of strings into a .csv file. The first column of the .csv contains the key names. The second contains the values. Will ignore any comments (denoted by #) or empty lines

Parameters:

csvfile : str

filename of the .csv file to read in

strip_quotes : str

if True, strip any double quotes from the strings before storing in the dictionary

Returns:

output_dict : dict

dictionary where the keys are the first column of the .csv and the values are the second column of the .csv

Notes

Reads from a a simple 2 column CSV file where the keys are stored in the first column and the values in the second.

Is not a strict .csv reader. Will skip any empty lines or lines without commas. Will also strip any comments denoted with # from the end of lines

cmind.utils.file_utils.dict2csv(input_dict, csvfile)[source]

write a simple dictionary into a .csv file containing strings or lists of strings. The first column of the .csv contains the key names. The second contains the values. If values are multiple comma-separated strings contained in “”, a list of strings will be stored

Parameters:

input_dict : dict

dictionary to store as .csv

csvfile : string

output filename

cmind.utils.file_utils.export_tarfile(filename, filelist, arcnames=None, dereference=True)[source]

utility for sending a list of files/directories to a .tar archive

Parameters:

filename : str

name of the tar file to create. It will be overwritten if it already exists. If the filename ends in .gz or .bz2, gzip or bz2 compression will be used, respectively.

filelist : list of str

the list of files to be added to the archive. This can also include directories.

arcnames : list of str, optional

optional list of the filenames to use inside the archive for each file in filelist

dereference : bool, optional

if True, replace any links with a copy of the linked file.

Returns:

filename : str

the file that was created

cmind.utils.file_utils.file_update_bool(input_file, output_file, NoCheck=False, remove_output_if_true=False)[source]

return a boolean that determines if output_file should be updated based on the relative date as compared to input_file

if input_file doesn’t exist, returns False if input_file exists, but output_file doesn’t return True if both exist, but input_file is newer return True if both exist, but output_fiel is newer return False if output_file is a directory return True
Parameters:

input_file : str

output_file : str

NoCheck : bool, optional

remove_output_if_true : bool, optional

cmind.utils.file_utils.filecheck_bool(input_file, output_file, NoCheck=False)[source]
Returns True if input_file exists, but output_file doesn’t exist
or if input_file is newer than output_file
Parameters:

input_file : str

input filename

output_file : str

output filename

NoCheck : bool, optional

if True, skip check of input file existence (default = False)

Returns:

filebool : bool

True if input_file exists and is newer than output_file

Notes

Intended for as a check to be used in cases where output_file should only be updated if it does not exist or if it has an older timestamp than the input_file.

cmind.utils.file_utils.imexist(image_filename, im_subset='all')[source]

Extension insensitive image match. optional second argument controls which file extensions are searched for

Parameters:

image_filename : str

image filename to check

im_subset : list or {‘all’,’nii’,’nifti’,’nifti-1’,’nrrd’,’img’,’analyze’,’png’,‘2dreport’,‘2dimg’}, optional

default is ‘all’, which tries to match the following extensions: [‘.nii.gz’,’.nii’,’.img’,’.hdr’,’.nrrd’,’.nhdr’]

Returns:

exists : bool

true if an image with the same basename as image_filename exists (specify extensions to search via im_subset)

true_fname : str

full path to the image file (with extension)

im_root : str

basename of the image file (without extension)

im_path : str

path containing the image file

im_ext : str

file extension (.e.g. .nii.gz)

Notes

possible string inputs to im_subset and their corresponding file extensions are:
  • all [.nii.gz,.nii,.img,.hdr,.nrrd,.nhdr] (default)
  • nifti-1 [.nii.gz,.nii]
  • nifti [.nii.gz,.nii]
  • nii [.nii.gz,.nii]
  • nrrd [.nrrd,.nhdr]
  • img [.img,.hdr]
  • analyze [.img,.hdr]
  • png [.png]
  • 2dreport [.gif,.png]
  • 2dimg [.gif,.jpg,.tif,.tiff,.eps,.png,.bmp]

alternatively, im_subset can be a list of the file extensions to consider

cmind.utils.file_utils.multiglob_list(strlist, basedir=None)[source]

concatenate results of multiple glob operations into a single list

Parameters:

strlist : list

list of strings to glob

basedir : str, optional

if provided, prepend basedir to the filenames in filelist

Returns

——-

filelist : str

list of files found

cmind.utils.file_utils.remove_file(f, warn=False)[source]

simple wrapper to call os.remove() only if the file exists

Parameters:

f : str

file to be removed

recursively remove any broken links within basedir via find/exec

Parameters:

basedir : str

remove all broken links within basedir (recursively)

use_shell : str, optional

if True, uses the linux find command. otherwise uses python builtins

cmind.utils.file_utils.split_multiple_ext(input_name)[source]

function to recursively remove multiple file extensions from a filename

Parameters:

input_name : str

filename to process

Returns:

input_name_base : str

base of input_name

ext : str

extension(s) of input_name

Notes

filename.nii.gz -> (filename, .nii.gz)

cmind.utils.file_utils.which(program)[source]

Search for a binary program on the system path this code from: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python

Parameters:

program : str

name of the executable to search for on the path

Returns:

path : str

path to the binary file corresponding to program