files.py 475 B

123456789101112131415161718
  1. import os
  2. def try_write(data, out_path, obj_name):
  3. """
  4. Tries to write a file out. Creates an exception if it fails.
  5. 'obj_name' is a name to give the thing you're trying to export so that
  6. if it fails, the error message that's given is more specific.
  7. """
  8. try:
  9. f = open(out_path, 'w')
  10. f.write(data)
  11. f.close()
  12. except IOError as e:
  13. raise Exception(f"Could not write {obj_name} to path '{out_path}'. More info: {e}")