Es. Python tutorial - Lavorare con i file
Versione italiana
Esercizi
1. Creazione e Scrittura in un File
Crea un file chiamato test.txt e scrivi dentro la frase:
"Hello, world!".
2. Lettura di un File
Leggi il contenuto del file test.txt e stampalo a
schermo.
3. Aggiunta di Testo a un File
Apri il file test.txt in modalità append e aggiungi la
frase: "Python è fantastico!".
4. Lettura Righe Singole da un File
Leggi il file test.txt riga per riga e stampale una alla
volta.
5. Uso di
with open
Apri e leggi il contenuto di test.txt utilizzando la
gestione contestuale with open().
6. Copia di un File
Crea una copia del file test.txt in un nuovo file
copy.txt.
7. Contare le Parole in un File
Leggi il file test.txt e conta quante parole
contiene.
8. Eliminare un File
Scrivi un programma che elimini test.txt se esiste.
9. Ricerca di una Parola in un File
Chiedi all’utente di inserire una parola e verifica se è presente in
test.txt.
10. Scrittura di una Lista in un File
Dati i seguenti nomi in una lista
["Alice", "Bob", "Charlie"], scrivili in un file
names.txt, un nome per riga.
Soluzioni
# 1. Creazione e Scrittura in un File
with open("test.txt", "w") as file:
file.write("Hello, world!")
# 2. Lettura di un File
with open("test.txt", "r") as file:
content = file.read()
print(content) # Output: Hello, world!
# 3. Aggiunta di Testo a un File
with open("test.txt", "a") as file:
file.write("\nPython è fantastico!")
# 4. Lettura Righe Singole da un File
with open("test.txt", "r") as file:
for line in file:
print(line.strip()) # Output: stampa ogni riga senza spazi extra
# 5. Uso di `with open`
with open("test.txt", "r") as file:
content = file.read()
print(content) # Output: Hello, world!\nPython è fantastico!
# 6. Copia di un File
with open("test.txt", "r") as original, open("copy.txt", "w") as copy:
copy.write(original.read())
# 7. Contare le Parole in un File
with open("test.txt", "r") as file:
words = file.read().split()
print(len(words)) # Output: Numero di parole nel file
# 8. Eliminare un File
import os
if os.path.exists("test.txt"):
os.remove("test.txt")
# 9. Ricerca di una Parola in un File
word_to_find = input("Inserisci una parola: ")
with open("test.txt", "r") as file:
content = file.read()
if word_to_find in content:
print("La parola è presente nel file.")
else:
print("La parola non è presente nel file.")
# 10. Scrittura di una Lista in un File
names = ["Alice", "Bob", "Charlie"]
with open("names.txt", "w") as file:
for name in names:
file.write(name + "\n")English version
Exercises
1. Creating and Writing to a File
Create a file called test.txt and write the sentence:
"Hello, world!" into it.
2. Reading a File
Read the contents of the file test.txt and print it to
the screen.
3. Appending Text to a File
Open the file test.txt in append mode and add the
sentence: "Python is awesome!".
4. Reading Single Lines from a File
Read the file test.txt line by line and print them one
at a time.
5. Using
with open
Open and read the contents of test.txt using the context
handler with open().
6. Copying a File
Create a copy of the file test.txt in a new file
copy.txt.
7. Count Words in a File
Read the file test.txt and count how many words it
contains.
8. Delete a File
Write a program that deletes test.txt if it exists.
9. Search for a Word in a File
Ask the user to enter a word and see if it is present in
test.txt.
10. Write a List to a File
Given the following names in a list
["Alice", "Bob", "Charlie"], write them to a file
names.txt, one name per line.
Solutions
#1. Creating and Writing to a File
with open("test.txt", "w") as file:
file.write("Hello, world!")
#2. Reading a File
with open("test.txt", "r") as file:
content = file.read()
print(content) # Output: Hello, world!
# 3. Adding Text to a File
with open("test.txt", "a") as file:
file.write("\nPython is awesome!")
# 4. Reading Single Lines from a File
with open("test.txt", "r") as file:
for line in file:
print(line.strip()) # Output: Print each line without extra spaces
# 5. Using `with open`
with open("test.txt", "r") as file:
content = file.read()
print(content) # Output: Hello, world!\nPython is awesome!
#6. Copy a File
with open("test.txt", "r") as original, open("copy.txt", "w") as copy:
copy.write(original.read())
#7. Count the Words in a File
with open("test.txt", "r") as file:
words = file.read().split()
print(len(words)) # Output: Number of words in the file
#8. Delete a File
import os
if os.path.exists("test.txt"):
os.remove("test.txt")
#9. Search for a Word in a File
word_to_find = input("Enter a word: ")
with open("test.txt", "r") as file:
content = file.read()
if word_to_find in content:
print("The word is present in the file.")
else:
print("The word is not present in the file.")
# 10. Writing a List to a File
names = ["Alice", "Bob", "Charlie"]
with open("names.txt", "w") as file:
for name in names:
file.write(name + "\n")
Commenti
Posta un commento