List files

The “List files” nodes reads a folder and creates a pandas DataFrame from its file list.

The folderPath parameter defines the folder to read.

Tip

A “List files” node which lists a folder containing subfolder can send them to another “List files” node. The folderPath parameter of the second node can be set to the column of the input DataFrame. This will make the second “List files” node to list all subfolders.

The filter parameter enables to list a subset of the folder files, using the Pattern language defined in Python (“List files” uses the Path.glob() method under the hood).

Note

The .DS_Store files are excluded automatically.

The columnName parameter defines the column name of the output DataFrame.

 1from pathlib import Path
 2import pandas
 3
 4class Tool:
 5    
 6    name = "List Files"
 7    description = "Reads a folder and creates a pandas DataFrame from the file list."
 8    environment = 'bioimageit'
 9    categories = ['Data']
10
11    inputs = [
12            dict(
13            name = 'folderPath',
14            help = 'Folder path',
15            type = 'Path',
16            autoColumn = True,
17            required = True,
18        ),
19        dict(
20            name = 'filter',
21            help = 'Filter the files',
22            type = 'str',
23            default = '*',
24        ),
25        dict(
26            name = 'columnName',
27            help = 'Column name',
28            type = 'str',
29            default = 'path',
30            static = True,
31        ),
32    ]
33    outputs = [
34    ]
35
36    def processDataFrame(self, dataFrame, argsList):
37        allFiles = []
38        for args in argsList:
39            if args.folderPath is None or not args.folderPath.is_dir(): continue
40            try:
41                files = sorted(list(args.folderPath.glob(args.filter))) if args.filter is not None and len(args.filter)>0 else None
42            except ValueError as e:
43                print(e)
44            if files is None:
45                files = sorted(list(args.folderPath.iterdir()))
46            allFiles += [f for f in files if f.name != '.DS_Store']
47        return pandas.DataFrame(data={argsList[0].columnName:allFiles} if len(argsList)>0 else {})