{"id":2371,"date":"2023-07-22T13:54:32","date_gmt":"2023-07-22T13:54:32","guid":{"rendered":"https:\/\/statorials.org\/it\/con-pitone-aperto\/"},"modified":"2023-07-22T13:54:32","modified_gmt":"2023-07-22T13:54:32","slug":"con-pitone-aperto","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/con-pitone-aperto\/","title":{"rendered":"Come utilizzare &quot;con&quot; in python per aprire file (inclusi esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare la seguente sintassi per aprire un file in Python, farci qualcosa e quindi chiudere il file:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>file = <span style=\"color: #008000;\">open<\/span> (' <span style=\"color: #ff0000;\">my_data.csv<\/span> ')\n\ndf = file. <span style=\"color: #3366ff;\">read<\/span> ()\n\n<span style=\"color: #008000;\">print<\/span> (df)\n\nfile. <span style=\"color: #3366ff;\">close<\/span> ()<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il problema con questo approccio \u00e8 che \u00e8 molto facile dimenticare di chiudere il file.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Un approccio migliore \u00e8 utilizzare <strong>open<\/strong> , che utilizza la seguente sintassi di base:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">with<\/span> <span style=\"color: #008000;\">open<\/span> (' <span style=\"color: #ff0000;\">my_data.csv<\/span> ') <span style=\"color: #008000;\">as<\/span> file:\n\n   df = file. <span style=\"color: #3366ff;\">read<\/span> ()\n\n   <span style=\"color: #008000;\">print<\/span> (df)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Utilizzando questo approccio, il file su cui stai lavorando viene chiuso automaticamente, quindi non devi ricordarti di utilizzare <strong>file.close()<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare <strong>open<\/strong> in diversi scenari.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: utilizzare l&#8217;istruzione With per leggere il file<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare l&#8217;istruzione &#8220;with&#8221; per leggere un file in Python e stampare il contenuto del file:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">with<\/span> <span style=\"color: #008000;\">open<\/span> (' <span style=\"color: #ff0000;\">my_data.csv<\/span> ') <span style=\"color: #008000;\">as<\/span> file:\n\n   df = file. <span style=\"color: #3366ff;\">read<\/span> ()\n\n   <span style=\"color: #008000;\">print<\/span> (df)\n\n,points, assists, rebounds\n0.11.5.6\n1,17,7,8\n2,16,7,8\n3,18,9,10\n4,22,12,14\n5,25,9,12\n6,26,9,12\n7,24,4,10\n8,29,8,11\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il contenuto del file viene stampato e il file viene chiuso automaticamente senza che venga digitato <strong>file.close()<\/strong> .<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: utilizzare l&#8217;istruzione With per scrivere un file<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare l&#8217;istruzione &#8220;with&#8221; per scrivere testo in un file:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">with<\/span> <span style=\"color: #008000;\">open<\/span> (' <span style=\"color: #ff0000;\">data_out.csv<\/span> ', ' <span style=\"color: #ff0000;\">w<\/span> ') <span style=\"color: #008000;\">as<\/span> file:\n\n    file. <span style=\"color: #3366ff;\">write<\/span> (' <span style=\"color: #ff0000;\">Some text to write to CSV file<\/span> ')\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Nota che la &#8216; <strong>w<\/strong> &#8216; nell&#8217;istruzione <strong>open()<\/strong> dice a Python di usare la modalit\u00e0 &#8216;scrittura&#8217; con il file invece della modalit\u00e0 lettura.<\/span><\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 3: utilizzare l&#8217;istruzione With per leggere e scrivere file<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo anche aprire pi\u00f9 file contemporaneamente in un&#8217;unica istruzione &#8220;with&#8221;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare l&#8217;istruzione &#8220;with&#8221; per aprire due file, leggere il contenuto di un file e quindi scrivere il contenuto del primo file nel secondo file:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">with<\/span> <span style=\"color: #008000;\">open<\/span> (' <span style=\"color: #ff0000;\">my_data.csv<\/span> ', ' <span style=\"color: #ff0000;\">r<\/span> ') <span style=\"color: #008000;\">as<\/span> infile, <span style=\"color: #008000;\">open<\/span> (' <span style=\"color: #ff0000;\">data_out.csv<\/span> ', ' <span style=\"color: #ff0000;\">w<\/span> ') <span style=\"color: #008000;\">as<\/span> outfile:\n    <span style=\"color: #008000;\">for<\/span> line <span style=\"color: #008000;\">in<\/span> infile:\n        outfile. <span style=\"color: #3366ff;\">write<\/span> (line)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Se andiamo alla posizione in cui abbiamo scritto &#8220;data_out.csv&#8221;, possiamo quindi visualizzare il contenuto del file:<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-21428 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/avec1.png\" alt=\"\" width=\"627\" height=\"302\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che possiamo utilizzare la funzione <strong>open()<\/strong> per aprire tutti i file che desideriamo in una singola istruzione &#8220;with&#8221;.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre operazioni comuni in Python:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-csv\/\" target=\"_blank\" rel=\"noopener\">Come leggere file CSV con Pandas<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-excel\/\" target=\"_blank\" rel=\"noopener\">Come leggere file Excel con Panda<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-il-file-di-testo\/\" target=\"_blank\" rel=\"noopener\">Come leggere file di testo con Pandas<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare la seguente sintassi per aprire un file in Python, farci qualcosa e quindi chiudere il file: file = open (&#8216; my_data.csv &#8216;) df = file. read () print (df) file. close () Il problema con questo approccio \u00e8 che \u00e8 molto facile dimenticare di chiudere il file. Un approccio migliore \u00e8 utilizzare open [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come usare &quot;with&quot; in Python per aprire file (inclusi esempi) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare l&#039;istruzione &quot;with&quot; in Python per aprire file, con diversi esempi.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/con-pitone-aperto\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come usare &quot;with&quot; in Python per aprire file (inclusi esempi) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare l&#039;istruzione &quot;with&quot; in Python per aprire file, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/con-pitone-aperto\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-22T13:54:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/avec1.png\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/con-pitone-aperto\/\",\"url\":\"https:\/\/statorials.org\/it\/con-pitone-aperto\/\",\"name\":\"Come usare &quot;with&quot; in Python per aprire file (inclusi esempi) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-22T13:54:32+00:00\",\"dateModified\":\"2023-07-22T13:54:32+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare l&#39;istruzione &quot;with&quot; in Python per aprire file, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/con-pitone-aperto\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/con-pitone-aperto\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/con-pitone-aperto\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come utilizzare &quot;con&quot; in python per aprire file (inclusi esempi)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come usare &quot;with&quot; in Python per aprire file (inclusi esempi) - Statorials","description":"Questo tutorial spiega come utilizzare l&#39;istruzione &quot;with&quot; in Python per aprire file, con diversi esempi.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/con-pitone-aperto\/","og_locale":"it_IT","og_type":"article","og_title":"Come usare &quot;with&quot; in Python per aprire file (inclusi esempi) - Statorials","og_description":"Questo tutorial spiega come utilizzare l&#39;istruzione &quot;with&quot; in Python per aprire file, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/con-pitone-aperto\/","og_site_name":"Statorials","article_published_time":"2023-07-22T13:54:32+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/avec1.png"}],"author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/con-pitone-aperto\/","url":"https:\/\/statorials.org\/it\/con-pitone-aperto\/","name":"Come usare &quot;with&quot; in Python per aprire file (inclusi esempi) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-22T13:54:32+00:00","dateModified":"2023-07-22T13:54:32+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare l&#39;istruzione &quot;with&quot; in Python per aprire file, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/con-pitone-aperto\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/con-pitone-aperto\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/con-pitone-aperto\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come utilizzare &quot;con&quot; in python per aprire file (inclusi esempi)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2371"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=2371"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2371\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}