- ¿Cómo comprobar que el archivo existe?
- ¿Cómo agregar un texto al archivo?
que sabe cómo crear el archivo, pero en este caso, se sobrescribe todos los datos:Agregar un texto al archivo en Python
import io
with open('text.txt', 'w', encoding='utf-8') as file:
file.write('text!')
En *nix
puedo hacer algo como:
#!/bin/sh
if [ -f text.txt ]
#If the file exists - append text
then echo 'text' >> text.txt;
#If the file doesn't exist - create it
else echo 'text' > text.txt;
fi;
'abierto ('text.txt', 'a', encoding = 'UTF-8')' – kev
Esto parece ser Python 3.x, ya que se utiliza el parámetro 'encoding' a' open() '. ¿Derecha? –
¡Utilicé la búsqueda y no pude encontrar la respuesta! – tomas