Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Errore quando apro uno dei file txt

J
Jacob97 (950 points)
0 6 14
in Programmare in python by (950 points)
recategorized by
Quando apro un file nel codice con with open(filename) as f: mi dà il seguente errore "a bytes-like object is required, not 'str'". Se specifico con mode='rt'  invece mi dà "can't have text and binary mode at once". Prima invece mi apriva tranquillamente il file, è un errore che è comparso senza che cambiassi nulla
471 views

2 Answers

Z
ZeroCrystal (4390 points)
8 14 46
by (4.4k points)

Per poter leggere un file come se fosse una stringa devi scrivere in questo modo: testo = open('file.txt').read().

(Di default un file viene aperto in modalità di sola lettura)

J
Jacob97 (950 points)
0 6 14
by (950 points)
Non è questo...l'ho scritto come te ma stesso errore
andrea.sterbini (207940 points)
754 1269 2377
by (208k points)
Che encoding usi?
J
Jacob97 (950 points)
0 6 14
by (950 points)
encoding='UTF8'