xcp_d.utils.filemanip module
Miscellaneous file manipulation functions.
- xcp_d.utils.filemanip.check_binary_mask(mask_file)[source]
Check if the mask is binary.
TODO: Fix non-binary mask bug in nibabies 22.1.3
- xcp_d.utils.filemanip.copyfile(originalfile, newfile, copy=False, create_new=False, hashmethod=None, use_hardlink=False, copy_related_files=True)[source]
Copy or link
originalfile
tonewfile
.If
use_hardlink
is True, and the file can be hard-linked, then a link is created, instead of copying the file.If a hard link is not created and
copy
is False, then a symbolic link is created.- Parameters:
originalfile (
str
) – full path to original filenewfile (
str
) – full path to new filecopy (bool) – specifies whether to copy or symlink files (default=False) but only for POSIX systems
use_hardlink (bool) – specifies whether to hard-link files, when able (Default=False), taking precedence over copy
copy_related_files (bool) – specifies whether to also operate on related files, as defined in
related_filetype_sets
- Returns:
newfile – The full path to the new file.
- Return type:
- xcp_d.utils.filemanip.copyfiles(filelist, dest, copy=False, create_new=False)[source]
Copy or symlink files in
filelist
todest
directory.- Parameters:
- Returns:
newfiles – List of new copied files.
- Return type:
- xcp_d.utils.filemanip.fname_presuffix(fname, prefix='', suffix='', newpath=None, use_ext=True)[source]
Manipulate path and name of input filename.
- Parameters:
fname (string) – A filename (may or may not include path)
prefix (string) – Characters to prepend to the filename
suffix (string) – Characters to append to the filename
newpath (string) – Path to replace the path of the input fname
use_ext (boolean) – If True (default), appends the extension of the original file to the output name.
- Returns:
Absolute path of the modified filename
- Return type:
Examples
>>> from nipype.utils.filemanip import fname_presuffix >>> fname = 'foo.nii.gz' >>> fname_presuffix(fname,'pre','post','/tmp') '/tmp/prefoopost.nii.gz'
>>> from nipype.interfaces.base import Undefined >>> fname_presuffix(fname, 'pre', 'post', Undefined) == fname_presuffix(fname, 'pre', 'post') True
Return a list of related files, as defined in
related_filetype_sets
, for a filename.For example, Nifti-Pair, Analyze (SPM), and AFNI files.
- xcp_d.utils.filemanip.hash_infile(afile, chunk_len=8192, crypto=<built-in function openssl_md5>, raise_notfound=False)[source]
Compute hash of a file using ‘crypto’ module.
Examples
>>> hash_infile('smri_ants_registration_settings.json') 'f225785dfb0db9032aa5a0e4f2c730ad'
>>> hash_infile('surf01.vtk') 'fdf1cf359b4e346034372cdeb58f9a88'
>>> hash_infile('spminfo') '0dc55e3888c98a182dab179b976dfffc'
>>> hash_infile('fsl_motion_outliers_fd.txt') 'defd1812c22405b1ee4431aac5bbdd73'
- xcp_d.utils.filemanip.on_cifs(fname)[source]
Check whether a file path is on a CIFS filesystem mounted in a POSIX host.
On Windows, Docker mounts host directories into containers through CIFS shares, which has support for Minshall+French symlinks, or text files that the CIFS driver exposes to the OS as symlinks. We have found that under concurrent access to the filesystem, this feature can result in failures to create or read recently-created symlinks, leading to inconsistent behavior and
FileNotFoundError
.This check is written to support disabling symlinks on CIFS shares.
- xcp_d.utils.filemanip.split_filename(fname)[source]
Split a filename into parts: path, base filename and extension.
- Parameters:
fname (
str
) – file or path name- Returns:
Examples
>>> from nipype.utils.filemanip import split_filename >>> pth, fname, ext = split_filename('/home/data/subject.nii.gz') >>> pth '/home/data'
>>> fname 'subject'
>>> ext '.nii.gz'
- xcp_d.utils.filemanip.which(cmd, env=None, pathext=None)[source]
Return the path to an executable which would be run if the given cmd was called.
If no cmd would be called, return
None
.Code for Python < 3.3 is based on a code snippet from http://orip.org/2009/08/python-checking-if-executable-exists-in.html