Created
August 26, 2022 13:59
-
-
Save damurashov/5d2eccfa0b7c5d7ed6064c404c607b74 to your computer and use it in GitHub Desktop.
Append to file, ensure it exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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