{"id":766,"date":"2024-04-19T11:40:23","date_gmt":"2024-04-19T11:40:23","guid":{"rendered":"https:\/\/javigomez.org\/?p=766"},"modified":"2025-09-29T20:32:51","modified_gmt":"2025-09-29T20:32:51","slug":"07-03-python-archivos-de-texto-con-with","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/04\/19\/07-03-python-archivos-de-texto-con-with\/","title":{"rendered":"07.03 Python \u2013 Archivos de texto con with"},"content":{"rendered":"\n<p>La cl\u00e1usula with en Python se puede utilizar para abrir y cerrar autom\u00e1ticamente un archivo. Esto es \u00fatil para evitar errores relacionados con el cierre de archivos. La cl\u00e1usula with toma un objeto de archivo y un bloque de c\u00f3digo. El objeto de archivo se abre al comienzo del bloque de c\u00f3digo y se cierra al final del bloque de c\u00f3digo, incluso si se produce una excepci\u00f3n.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"a.txt\",\"a\",encoding=\"utf-8\") as archivo:\n    archivo.write(\"L\u00ednea 3\")\n\n\n\nwith open(\"a.txt\",\"r\",encoding=\"utf-8\") as archivo:\n    contenido=archivo.read()\n    print(contenido)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"a.txt\",\"r\",encoding=\"utf-8\") as archivo:\n    contenido=archivo.read()\n    print(contenido)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"a.txt\",\"r\",encoding=\"utf-8\") as archivo:\n    contenido=archivo.read(3)\n    print(contenido)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"a.txt\",\"r\",encoding=\"utf-8\") as archivo:\n    contenido=archivo.readline()\n    print(contenido)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"a.txt\",\"r\",encoding=\"utf-8\") as archivo:\n    for linea in archivo:\n        print(linea)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"a.txt\",\"r\",encoding=\"utf-8\") as archivo:\n    contenido=archivo.readlines()\n    print(contenido)\n    for ele in contenido:\n        print (ele)\n<\/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 03 Archivos with\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/AZUt8snfzBs?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>La cl\u00e1usula with en Python se puede utilizar para abrir y cerrar autom\u00e1ticamente un archivo. Esto es \u00fatil para evitar errores relacionados con el cierre &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,351],"class_list":["post-766","post","type-post","status-publish","format-standard","hentry","category-python","tag-ficheros","tag-python","tag-with"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/766","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=766"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/766\/revisions"}],"predecessor-version":[{"id":847,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/766\/revisions\/847"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}