imrm.py 1.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
#
# imrm.py - Remove image files.
#
# Author: Paul McCarthy <paulmc@fmrib.ox.ac.uk>
#
"""This module defines the ``imrm`` application, for removing NIFTI image
files.
"""


import itertools      as it
import os.path        as op
import                   os
import                   sys
import fsl.utils.path as fslpath


19
usage = """Usage: imrm <list of image names to remove>
20
21
22
23
NB: filenames can be basenames or not
""".strip()


24
25
26
27
28
29
30
31
# This list is defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
        '.img',    '.hdr',
        '.img.gz', '.hdr.gz',
        '.mnc',    '.mnc.gz']
32
33
34
35
36
37
38
"""List of file extensions that are removed by ``imrm``. """


def main(argv=None):
    """Removes all images which are specified on the command line. """

    if argv is None:
39
        argv = sys.argv[1:]
40

41
42
    if len(argv) < 1:
        print(usage)
43
44
        return 1

45
    prefixes = [fslpath.removeExt(p, exts) for p in argv]
46

47
    for prefix, ext in it.product(prefixes, exts):
48
49
50
51
52
53
54
55
56
57
58

        path = f'{prefix}{ext}'

        if op.exists(path):
            os.remove(path)

    return 0


if __name__ == '__main__':
    sys.exit(main())