prog:fichiers
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
prog:fichiers [2020/03/19 10:15] – jbpuel | prog:fichiers [2020/04/06 09:03] (Version actuelle) – supprimée jbpuel | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Les fichiers en Python ====== | ||
- | |||
- | ===== Fichiers texte ===== | ||
- | |||
- | <code python> | ||
- | # Lecture d'un fichier ligne par ligne | ||
- | chemin = " | ||
- | with open(chemin, | ||
- | lignes = f.readlines() # Conserve les \n | ||
- | # lignes = f.read().splitlines() # Ne conserve pas les \n | ||
- | print (lignes) | ||
- | # f.seek(0) repositionne le pointeur en position 0 du fichier | ||
- | |||
- | # Pareil mais avec une boucle ligne par ligne | ||
- | chemin = " | ||
- | with open(chemin, | ||
- | for ligne in f: | ||
- | print (ligne) | ||
- | |||
- | # Ecriture dans un fichier | ||
- | chemin = " | ||
- | # Mode " | ||
- | with open(chemin, | ||
- | f.write(" | ||
- | | ||
- | # Autre possibilité : utiliser open comme une fonction | ||
- | chemin = " | ||
- | f = open(chemin, | ||
- | f.close() # Indispensable si on utilise open comme une fonction | ||
- | </ | ||
- | |||
- | ===== Fichiers Json ===== | ||
- | |||
- | |||
prog/fichiers.1584612906.txt.gz · Dernière modification : 2020/03/19 10:15 de jbpuel