<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Ciao,<br>
</p>
<p> ci sarà un altro errore che fa scattare l'except. Prova ad
eliminare il try/except ed esegui solo il blocco nel try, dovresti
vedere l'errore.</p>
<p> Solitamente conviene scrivere l'eccezione in maniera esplicita:</p>
<p> except OSError:</p>
<p> Se non è un errore sul file, allora vedrai il traceback e
l'errore reale.<br>
</p>
<div class="moz-cite-prefix">Il 26/05/22 16:05, Gabriele Battaglia
ha scritto:<br>
</div>
<blockquote type="cite"
cite="mid:183980ac-0d6b-c379-6105-6b4cbcbc7c37@libero.it">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<p>Ciao di nuovo.</p>
<p><br>
</p>
<p>Ho questo codice:</p>
<p><br>
</p>
<div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 10px;line-height: 14px;white-space: pre;"><div><span style="color: #9cdcfe;">fn</span><span style="color: #d4d4d4;"> = </span><span style="color: #dcdcaa;">input</span><span style="color: #d4d4d4;">(</span><span style="color: #ce9178;">"filename.ext to prepare? > "</span><span style="color: #d4d4d4;">)</span></div><div><span style="color: #c586c0;">try</span><span style="color: #d4d4d4;">:</span></div><div><span style="color: #d4d4d4;"> </span><span style="color: #9cdcfe;">f</span><span style="color: #d4d4d4;">=</span><span style="color: #dcdcaa;">open</span><span style="color: #d4d4d4;">(</span><span style="color: #9cdcfe;">fn</span><span style="color: #d4d4d4;">, </span><span style="color: #ce9178;">"rt"</span><span style="color: #d4d4d4;">)</span></div><div><span style="color: #d4d4d4;"> </span><span style="color: #9cdcfe;">rig</span><span style="color: #d4d4d4;"> = </span><span style="color: #9cdcfe;">f</span><span style="color: #d4d4d4;">.</span><span style="color: #dcdcaa;">readlines</span><span style="color: #d4d4d4;">()</span></div><div><span style="color: #d4d4d4;"> </span><span style="color: #9cdcfe;">f</span><span style="color: #d4d4d4;">.</span><span style="color: #dcdcaa;">close</span><span style="color: #d4d4d4;">()</span></div><div><span style="color: #d4d4d4;"> </span><span style="color: #dcdcaa;">print</span><span style="color: #d4d4d4;">(</span><span style="color: #569cd6;">f</span><span style="color: #ce9178;">"File: </span><span style="color: #569cd6;">{</span><span style="color: #9cdcfe;">fn</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> found, with </span><span style="color: #569cd6;">{</span><span style="color: #dcdcaa;">len</span><span style="color: #d4d4d4;">(</span><span style="color: #9cdcfe;">rig</span><span style="color: #d4d4d4;">)</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> lines of text."</span><span style="color: #d4d4d4;">)</span></div><div><span style="color: #c586c0;">except</span><span style="color: #d4d4d4;">:</span></div><div><span style="color: #d4d4d4;"> </span><span style="color: #dcdcaa;">print</span><span style="color: #d4d4d4;">(</span><span style="color: #569cd6;">f</span><span style="color: #ce9178;">"Sorry, file: </span><span style="color: #569cd6;">{</span><span style="color: #9cdcfe;">fn</span><span style="color: #569cd6;">}</span><span style="color: #ce9178;"> not found.</span><span style="color: #d7ba7d;">\n</span><span style="color: #ce9178;">Relaunch the App and try with another filename."</span><span style="color: #d4d4d4;">)</span></div><div><span style="color: #d4d4d4;"> </span><span style="color: #4ec9b0;">sys</span><span style="color: #d4d4d4;">.</span><span style="color: #dcdcaa;">exit</span><span style="color: #d4d4d4;">()</span></div>
Il file è certamente presente nella cartella dello script, ne sono ASSOLUTAMENTE certo. Eppure, non trova il file che gli chiedo.</div>
<div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 10px;line-height: 14px;white-space: pre;">Magari ne trova altri, ma quello che mi interessa, solleva l'eccezione e termina lo script. Quali ragioni possono esserci</div>
<div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 10px;line-height: 14px;white-space: pre;">Si tratta di tutti file di testo.</div>
<div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 10px;line-height: 14px;white-space: pre;">Il nome digitato è corretto.</div>
<div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 10px;line-height: 14px;white-space: pre;">
</div>
<div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 10px;line-height: 14px;white-space: pre;">Gabry</div>
<div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 10px;line-height: 14px;white-space: pre;">
</div>
<pre class="moz-signature" cols="72">--
--... ...-- -.. . .. --.. ....- .- .--. ..- - ..- . .
Sent from TB on Windows 10, Genus Bononiae's computer.</pre>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
Python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python@lists.python.it">Python@lists.python.it</a>
<a class="moz-txt-link-freetext" href="https://lists.python.it/mailman/listinfo/python">https://lists.python.it/mailman/listinfo/python</a>
</pre>
</blockquote>
</body>
</html>