Texto, texto, texto, pero no binario

--Originally published at Mike's Blog

Python tiene la capacidad de poder abrir y editar archivos que sean de texto solamente. Como son los formatos .txt y .csv. Estos solo tienen valores, y no tienen ningún código binario de formato como lo tienen los archivos .doc o .docx de Microsoft Word.

La sintaxis de este comando es extremadamente facil.

Por ejemplo, para poder asignar el texto de un archivo a una variable se utiliza este comando: f=open(“archivo.txt”).

Lo que hace esto es asignar a “f” el texto del archivo.

ascreenshot-from-201m6-10-24-10-52-41 screenshot-from-2016-10-24-10-52-33

Pero tambien con Python podemos escribir en un archivo de texto…

file = open(“newfile.txt”, “w”)

file.write(“hello world in the new file”)

file.write(” and another line”)

file.close()

En este caso en el archivo newfile.txt, vamos a escribir “hello world in the new file and another line”

screenshot-from-2016-10-24-10-59-25 screenshot-from-2016-10-24-10-59-39

http://opentechschool.github.io/python-data-intro/core/text-files.html

 

giphy