Skip to content

Instantly share code, notes, and snippets.

@damurashov
Created August 26, 2022 13:59
Show Gist options
  • Save damurashov/5d2eccfa0b7c5d7ed6064c404c607b74 to your computer and use it in GitHub Desktop.
Save damurashov/5d2eccfa0b7c5d7ed6064c404c607b74 to your computer and use it in GitHub Desktop.
Append to file, ensure it exists
import os
import pathlib
def file_append(appendix, fname):
if not os.path.exists(fname):
mode = 0o774
path = pathlib.Path(str(fname)).parent.resolve()
if pathlib.Path(__file__).parent.resolve() != path and not os.path.exists(path):
os.makedirs(str(path), mode)
with open(fname, 'w') as f:
pass
with open(fname, "a") as f:
f.write(appendix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment