Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
intro
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Corsi
Slides Corsi Python
intro
Commits
ce48fa44
Commit
ce48fa44
authored
May 08, 2017
by
Niccolò Izzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added files and exceptions.
parent
d3aa58aa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
107 additions
and
2 deletions
+107
-2
day_1.md
day_1.md
+107
-2
No files found.
day_1.md
View file @
ce48fa44
...
...
@@ -1034,9 +1034,114 @@ Gattuso
----
## A
ccesso a
i file
## A
prire
i file
---
```
python
f
=
open
(
"documento.txt"
,
"w"
)
f
.
write
(
"contenuto"
)
f
.
close
()
```
-
`open(filename, mode)`
ritorna un riferimento al file
-
Il parametro
`mode`
specifica la modalità
| mode | descrizione |
| :---- | :------------------------------ |
| r | Apertura in sola lettura |
| w | Apertura in sola scrittura |
| r+ | Apertura in lettura e scrittura |
| a | Scrittura in coda |
----
## Usare i file
-
Per scrivere su un file
```
>>> f = open("documento.txt", "w")
>>> f.write("Contenuti serissimi.")
20
>>> f.close()
```
-
Per leggere da un file
```
>>> f = open("documento.txt", "r")
>>> f.read()
'Contenuti serissimi.'
>>> f.close()
```
-
I metodi
`readline`
e
`writeline`
leggono e scrivono una riga alla volta
-
Ricordiamoci di chiudere sempre il file con la funzione
`close()`
----
## Una scorciatoia
```
python
with
open
(
"documento.txt"
,
"w"
)
as
f
:
f
.
write
(
"contenuto"
)
```
-
All'interno del blocco,
`f`
è un riferimento al file
-
Non serve chiudere esplicitamente
-
Se vengono sollevati errori o eccezioni il file viene chiuso
----
## Guardiamoci le spalle
-
Cosa succede apriamo un file che non esiste?
```
>>> f = open("documento.rtf", "r")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory ...
```
-
L'interprete solleva un errore
-
Abbiamo la possibilità di prevenire un problema!
----
## Errori
-
Sono condizioni eccezionali che possono verificarsi
-
Vanno gestite con blocchi
`try ... except`
```
>>> try:
... f = open("documento.txt", "r")
... except FileNotFoundError:
... print("Acciderba, questo file non esiste! Lo creo.")
... f = open("documento.txt", "w")
```
-
Possiamo lanciarli a nostra volta
```
>>> raise Exception("No internet connection!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: No internet connection!
```
----
## `import this`
----
## Nella prossima puntata
-
Programmazione funzionale
-
Lambda
-
List comprehensions
-
Generatori
-
Programmazione orientata agli oggetti
-
Classi
-
Metodi
-
Ereditarietà
----
# Thank you!
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment