from pathlib import Path Path(filename).write_text(any_string) Path(filename).write_bytes(any_binary_data)