diff options
Diffstat (limited to 'buildscripts/util')
-rw-r--r-- | buildscripts/util/fileops.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/buildscripts/util/fileops.py b/buildscripts/util/fileops.py index e56348d8548..e26e67c6593 100644 --- a/buildscripts/util/fileops.py +++ b/buildscripts/util/fileops.py @@ -25,3 +25,30 @@ def get_file_handle(path, append_file=False): """Open 'path', truncate it if 'append_file' is False, and return file handle.""" mode = "a+" if append_file else "w" return open(path, mode) + + +def write_file(path: str, contents: str) -> None: + """ + Write the contents provided to the file in the specified path. + + :param path: Path of file to write. + :param contents: Contents to write to file. + """ + with open(path, "w") as file_handle: + file_handle.write(contents) + + +def write_file_to_dir(directory: str, file: str, contents: str) -> None: + """ + Write the contents provided to the file in the given directory. + + The directory will be created if it does not exist. + + :param directory: Directory to write to. + :param file: Name of file to write. + :param contents: Contents to write to file. + """ + if not os.path.exists(directory): + os.makedirs(directory) + + write_file(os.path.join(directory, file), contents) |