Writes string file contents to file, clearing contents of the file on first write and then appending on subsequent calls. Args: file_content: string, the contents
(self, file_content)
| 749 | return result |
| 750 | |
| 751 | def write(self, file_content): |
| 752 | """Writes string file contents to file, clearing contents of the file |
| 753 | on first write and then appending on subsequent calls. |
| 754 | |
| 755 | Args: |
| 756 | file_content: string, the contents |
| 757 | """ |
| 758 | if not self.write_mode: |
| 759 | raise errors.PermissionDeniedError( |
| 760 | None, None, "File not opened in write mode" |
| 761 | ) |
| 762 | if self.closed: |
| 763 | raise errors.FailedPreconditionError( |
| 764 | None, None, "File already closed" |
| 765 | ) |
| 766 | |
| 767 | if self.fs_supports_append: |
| 768 | if not self.write_started: |
| 769 | # write the first chunk to truncate file if it already exists |
| 770 | self.fs.write(self.filename, file_content, self.binary_mode) |
| 771 | self.write_started = True |
| 772 | |
| 773 | else: |
| 774 | # append the later chunks |
| 775 | self.fs.append(self.filename, file_content, self.binary_mode) |
| 776 | else: |
| 777 | # add to temp file, but wait for flush to write to final filesystem |
| 778 | if self.write_temp is None: |
| 779 | mode = "w+b" if self.binary_mode else "w+" |
| 780 | self.write_temp = tempfile.TemporaryFile(mode) |
| 781 | |
| 782 | compatify = compat.as_bytes if self.binary_mode else compat.as_text |
| 783 | self.write_temp.write(compatify(file_content)) |
| 784 | |
| 785 | def __next__(self): |
| 786 | line = None |