Aller au contenu principal

I/O Fichiers

#Fichiers (I/O)

Pyodide fournit un système de fichiers virtuel en mémoire.

pythonpython
1with open('demo.txt','w', encoding='utf-8') as f:2    f.write('demo')3print(open('demo.txt', encoding='utf-8').read())

Lecture ligne à ligne et gestion d’erreurs:

pythonpython
1try:2    with open('donnees.csv', encoding='utf-8') as f:3        for line in f:4            print(line.strip())5except FileNotFoundError:6    print('Fichier absent — créez-le d\'abord')

#Playground

#Exercices

  • Lire un CSV simple a,b,c et calculer la moyenne de la colonne c.
  • Écrire un mini journaliseur qui ajoute des lignes datées dans un fichier texte.