{"id":764,"date":"2024-04-19T11:36:45","date_gmt":"2024-04-19T11:36:45","guid":{"rendered":"https:\/\/javigomez.org\/?p=764"},"modified":"2025-09-29T20:32:51","modified_gmt":"2025-09-29T20:32:51","slug":"07-02-python-archivos-de-texto","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/04\/19\/07-02-python-archivos-de-texto\/","title":{"rendered":"07.02 Python \u2013 Archivos de texto"},"content":{"rendered":"\n<p>Se pueden leer y escribir archivos de texto utilizando las funciones open(), read(), write() y close().<\/p>\n\n\n\n<p>La funci\u00f3n <strong>open<\/strong>() se utiliza para abrir un archivo y devolver un objeto de archivo. El objeto de archivo se puede utilizar para leer y escribir en el archivo.<\/p>\n\n\n\n<p>La funci\u00f3n <strong>read<\/strong>() se utiliza para leer el contenido de un archivo y devolverlo como una cadena.<\/p>\n\n\n\n<p>La funci\u00f3n <strong>write<\/strong>() se utiliza para escribir una cadena en un archivo.<\/p>\n\n\n\n<p>La funci\u00f3n <strong>close<\/strong>() se utiliza para cerrar un archivo y liberar los recursos asociados con el archivo.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>archivo=open(\"a.txt\",\"a\",encoding=\"utf-8\")\narchivo.write(\"L\u00ednea 2\\n\")\narchivo.close()\n\narchivo=open(\"a.txt\",\"r\",encoding=\"utf-8\")\ncontenido=archivo.read()\nprint(\"CONTENIDO: \", contenido)\narchivo.close()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    archivo=open(\"a.txt\",\"r\",encoding=\"utf-8\")\n    contenido=archivo.read()\n    print(\"CONTENIDO: \", contenido)\n    archivo.close()\nexcept Exception as e:\n    print (\"ERROR: \", e)\nelse:\n    archivo.close()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"PYTHON U07 02 Archivos\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/yqgUNgRycFc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Se pueden leer y escribir archivos de texto utilizando las funciones open(), read(), write() y close(). La funci\u00f3n open() se utiliza para abrir un archivo &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[144,277],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-python","tag-ficheros","tag-python"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":848,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions\/848"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}