utils.py 634 B

1234567891011121314151617181920212223
  1. import os
  2. import re
  3. EMPTY_ANNOTATION_FILE = ('<?xml version="1.0" encoding="UTF-8" ?>'
  4. '<document><annotations></annotations></document>')
  5. def sanitize_identifier(identifier, replacement='-'):
  6. return re.sub(r'[^\w-]', replacement, identifier)
  7. def check_is_file_empty(filepath):
  8. """
  9. Check whether file is empty or not.
  10. :param filepath: Path of a file that will be checked.
  11. :return: True if the file empty.
  12. """
  13. if os.path.exists(filepath):
  14. return os.stat(filepath).st_size == 0
  15. else:
  16. raise FileNotFoundError("Path '%s' doesn't exist" % filepath)