Reads contents of file to a string. Args: n: int, number of bytes or characters to read, otherwise read all the contents of the file Returns: Subset of the contents of the file as a string or bytes.
(self, n=None)
| 704 | return self.buff[old_buff_offset : old_buff_offset + read_size] |
| 705 | |
| 706 | def read(self, n=None): |
| 707 | """Reads contents of file to a string. |
| 708 | |
| 709 | Args: |
| 710 | n: int, number of bytes or characters to read, otherwise |
| 711 | read all the contents of the file |
| 712 | |
| 713 | Returns: |
| 714 | Subset of the contents of the file as a string or bytes. |
| 715 | """ |
| 716 | if self.write_mode: |
| 717 | raise errors.PermissionDeniedError( |
| 718 | None, None, "File not opened in read mode" |
| 719 | ) |
| 720 | |
| 721 | result = None |
| 722 | if self.buff and len(self.buff) > self.buff_offset: |
| 723 | # read from local buffer |
| 724 | if n is not None: |
| 725 | chunk = self._read_buffer_to_offset(self.buff_offset + n) |
| 726 | if len(chunk) == n: |
| 727 | return chunk |
| 728 | result = chunk |
| 729 | n -= len(chunk) |
| 730 | else: |
| 731 | # add all local buffer and update offsets |
| 732 | result = self._read_buffer_to_offset(len(self.buff)) |
| 733 | |
| 734 | # read from filesystem |
| 735 | read_size = max(self.buff_chunk_size, n) if n is not None else None |
| 736 | (self.buff, self.continuation_token) = self.fs.read( |
| 737 | self.filename, self.binary_mode, read_size, self.continuation_token |
| 738 | ) |
| 739 | self.buff_offset = 0 |
| 740 | |
| 741 | # add from filesystem |
| 742 | if n is not None: |
| 743 | chunk = self._read_buffer_to_offset(n) |
| 744 | else: |
| 745 | # add all local buffer and update offsets |
| 746 | chunk = self._read_buffer_to_offset(len(self.buff)) |
| 747 | result = result + chunk if result else chunk |
| 748 | |
| 749 | return result |
| 750 | |
| 751 | def write(self, file_content): |
| 752 | """Writes string file contents to file, clearing contents of the file |