Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

utils.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. # -----------------------------------------------------------------------------
  4. # File: utils.py (as part of project URUMETRICS)
  5. # Created: 04/05/2022 15:47
  6. # Last Modified: 04/05/2022 15:47
  7. # -----------------------------------------------------------------------------
  8. # Author: William N. Havard
  9. # Postdoctoral Researcher
  10. #
  11. # Mail : william.havard@ens.fr / william.havard@gmail.com
  12. #
  13. # Institution: ENS / Laboratoire de Sciences Cognitives et Psycholinguistique
  14. #
  15. # ------------------------------------------------------------------------------
  16. # Description:
  17. # •
  18. # -----------------------------------------------------------------------------
  19. import os
  20. def get_raw_filename(fp):
  21. """
  22. Return the raw filename of file given its path (removes path and extension)
  23. :param fp: file path
  24. :type fp: str
  25. :return: raw filename
  26. :rtype: str
  27. """
  28. return os.path.basename(os.path.splitext(fp)[0])
  29. def walk_dir(path, ext=[], return_full_path=True):
  30. """
  31. Recursively explore a directory and return all the files that end with a given extension. If no extension is
  32. specified, all the files are returned
  33. :param path: path to the directory to explore
  34. :type path: str
  35. :param ext: extensions that should be considered
  36. :type ext: list of str or str
  37. :param return_full_path: should the full path to the files be returned
  38. :type return_full_path: bool
  39. :return: list of files
  40. :rtype: list of str
  41. """
  42. if type(ext) == str: ext = [ext]
  43. files = []
  44. for p, d, f in os.walk(path):
  45. for ff in f:
  46. _, file_extension = os.path.splitext(ff)
  47. if not len(ext) or file_extension[1:] in ext:
  48. path_suffix = p.replace(os.path.commonprefix([path, p]), '').lstrip(os.sep)
  49. if return_full_path:
  50. files.append(os.path.join(path, path_suffix, ff))
  51. else:
  52. files.append(os.path.join(path_suffix, ff))
  53. return sorted(files)