Traducción funciones de php file_get_contents y file_put_contents en python

Traduzco dos funciones que utilizo constantemente en PHP
Dos funciones que utilizo mucho en PHP para lectura y escritura de archivos.
file_get_contents
def file_get_contents(pathfile):
try:
with open(pathfile) as f:
return f.read()
except IOError:
return f"no file found: {pathfile}"
file_put_contents
def file_put_contents(pathfile, strdata=""):
try:
with open(pathfile, 'w') as f:
f.write(strdata)
except IOError:
return f"no file found: {pathfile}"
# version con append data. Agrega el contenido al archivo sin eliminar lo que ya había.
def file_put_contents(pathfile, strdata=""):
try:
with open(pathfile, "a") as f:
f.write(strdata)
except IOError:
return f"no file found: {pathfile}"
Autor: Eduardo A. F.
Publicado: 19-09-2020 00:00
Actualizado: 16-02-2022 12:25