@@ -263,6 +263,82 @@ def derive_out_dir(in_dir, out_dir):
263263 return out_dir
264264
265265
266+ def list_dirs_containing_filetype (source , filetype ):
267+ """List all directories (recursive) that contain files with the given suffix.
268+
269+ Parameters
270+ ----------
271+ source : str
272+ Path to source dir.
273+ filetype : str
274+ Filetype (string pattern) that should be matched against filenames in
275+ the directories.
276+
277+ Returns
278+ -------
279+ list(str)
280+ List of all dirs that contain files of the given filetype.
281+ """
282+ dirs_containing_filetype = []
283+
284+ # walk recursively through all directories
285+ # list their paths and all files inside (=os.walk)
286+ for dirname , _ , filenames in os .walk (source ):
287+ # stop when encountering a directory that contains "filetype"
288+ # and store the directory path
289+ for filename in filenames :
290+ if filetype in filename :
291+ dirs_containing_filetype .append (dirname + "/" )
292+ break
293+
294+ return dirs_containing_filetype
295+
296+
297+ def list_all_filenames (source , filetype ):
298+ """Get a sorted list of all files of specified filetype in a given directory.
299+
300+ Parameters
301+ ----------
302+ source : str
303+ Path to the directory to scan for desired files.
304+ filetype : str
305+ The file extension to look for.
306+
307+ Returns
308+ -------
309+ list(str)
310+ List of all files with the given suffix in the source directory.
311+ """
312+ os .chdir (str (source ))
313+ allimages = sorted (glob .glob ("*" + filetype )) # sorted by name
314+
315+ return allimages
316+
317+
318+ def get_folder_size (source ):
319+ """Get the total size of a given directory and its subdirectories.
320+
321+ Parameters
322+ ----------
323+ source : str
324+ Directory for which the size should be determined.
325+
326+ Returns
327+ -------
328+ int
329+ The total size of all files in the source dir and subdirs in bytes.
330+ """
331+ total_size = 0
332+ for dirpath , _ , filenames in os .walk (source ):
333+ for fname in filenames :
334+ fpath = os .path .join (dirpath , fname )
335+ # skip if it is symbolic link
336+ if not os .path .islink (fpath ):
337+ total_size += os .path .getsize (fpath )
338+
339+ return total_size
340+
341+
266342# pylint: disable-msg=C0103
267343# we use the variable name 'exists' in its common spelling (lowercase), so
268344# removing this workaround will be straightforward at a later point
0 commit comments