Writes to `filename` atomically. This means that when `filename` appears in the filesystem, it will contain all of `contents`. With write_string_to_file, it is possible for the file to appear in the filesystem with `contents` only partially written. Accomplished by writing to a temp file a
(filename, contents, overwrite=True)
| 520 | |
| 521 | |
| 522 | def atomic_write_string_to_file(filename, contents, overwrite=True): |
| 523 | """Writes to `filename` atomically. |
| 524 | |
| 525 | This means that when `filename` appears in the filesystem, it will contain |
| 526 | all of `contents`. With write_string_to_file, it is possible for the file |
| 527 | to appear in the filesystem with `contents` only partially written. |
| 528 | |
| 529 | Accomplished by writing to a temp file and then renaming it. |
| 530 | |
| 531 | Args: |
| 532 | filename: string, pathname for a file |
| 533 | contents: string, contents that need to be written to the file |
| 534 | overwrite: boolean, if false it's an error for `filename` to be occupied by |
| 535 | an existing file. |
| 536 | """ |
| 537 | temp_pathname = filename + ".tmp" + uuid.uuid4().hex |
| 538 | write_string_to_file(temp_pathname, contents) |
| 539 | try: |
| 540 | rename(temp_pathname, filename, overwrite) |
| 541 | except errors.OpError: |
| 542 | delete_file(temp_pathname) |
| 543 | raise |
| 544 | |
| 545 | |
| 546 | @tf_export(v1=["gfile.DeleteRecursively"]) |
nothing calls this directly
no test coverage detected