Files
Python ↔ Baobab
9 correspondences
ouvrir() maps to open(), avec…comme to with…as. Modes are French: "l" for read (r), "e" for write (w), "a" for append (a).
| Python | Baobab | Description |
|---|---|---|
| open(chemin, "r") | ouvrir(chemin, "l") | Open for reading. |
| open(chemin, "w") | ouvrir(chemin, "e") | Open for writing (overwrites content). |
| open(chemin, "a") | ouvrir(chemin, "a") | Open for appending. |
| with open(…) as f: | avec ouvrir(…) comme f: | Automatic file closing. |
| f.read() | f.lire() | Read the whole content as one string. |
| f.readlines() | f.lire_lignes() | Read all lines into a list. |
| f.write(texte) | f.ecrire(texte) | Write a string into the file. |
| for ligne in f: | pour ligne dans f: | Iterate line by line (memory-efficient). |
| f.close() | f.fermer() | Manual close (prefer avec…comme). |
Complete example — Baobab ↔ Python
Baobab
12345678910111213141516171819202122232425# Écriture
avec ouvrir("notes.txt", "e") comme f:
f.ecrire("Amina: 15\n")
f.ecrire("Kofi: 12\n")
# Ajout
avec ouvrir("notes.txt", "a") comme f:
f.ecrire("Fatou: 18\n")
# Lecture complète
avec ouvrir("notes.txt", "l") comme f:
contenu = f.lire()
afficher(contenu)
# Lecture ligne par ligne
avec ouvrir("notes.txt", "l") comme f:
pour ligne dans f:
afficher(ligne.rogner())
# Gestion du fichier manquant
essayer:
avec ouvrir("absent.txt", "l") comme f:
f.lire()
attraper ErreurFichierIntrouvable:
afficher("Le fichier n'existe pas")Python
12345678910111213141516171819202122232425# Writing
with open("notes.txt", "w") as f:
f.write("Amina: 15\n")
f.write("Kofi: 12\n")
# Appending
with open("notes.txt", "a") as f:
f.write("Fatou: 18\n")
# Full read
with open("notes.txt", "r") as f:
contenu = f.read()
print(contenu)
# Line by line
with open("notes.txt", "r") as f:
for ligne in f:
print(ligne.strip())
# Missing file handling
try:
with open("absent.txt", "r") as f:
f.read()
except FileNotFoundError:
print("Le fichier n'existe pas")