Reads the entire contents of a file to a string. Args: filename: string, path to a file binary_mode: whether to open the file in binary mode or not. This changes the type of the object returned. Returns: contents of the file as a string or bytes. Raises: errors.OpError
(filename, binary_mode=False)
| 312 | |
| 313 | |
| 314 | def read_file_to_string(filename, binary_mode=False): |
| 315 | """Reads the entire contents of a file to a string. |
| 316 | |
| 317 | Args: |
| 318 | filename: string, path to a file |
| 319 | binary_mode: whether to open the file in binary mode or not. This changes |
| 320 | the type of the object returned. |
| 321 | |
| 322 | Returns: |
| 323 | contents of the file as a string or bytes. |
| 324 | |
| 325 | Raises: |
| 326 | errors.OpError: Raises variety of errors that are subtypes e.g. |
| 327 | NotFoundError etc. |
| 328 | """ |
| 329 | if binary_mode: |
| 330 | f = FileIO(filename, mode="rb") |
| 331 | else: |
| 332 | f = FileIO(filename, mode="r") |
| 333 | return f.read() |
| 334 | |
| 335 | |
| 336 | def write_string_to_file(filename, file_content): |