Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| python:filesystem [2019/02/25 12:44] – créée phsw | python:filesystem [2024/03/27 10:23] (Version actuelle) – add how to create temporary file phsw | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | ====== Manipulation du système de fichiers ====== | + | ====== Manipulation du système de fichiers |
| - | ==== Concaténer des chemins | + | ==== Manipulation sur les chemins ==== |
| + | |||
| + | === Concaténer des chemins === | ||
| <code python> | <code python> | ||
| Ligne 10: | Ligne 12: | ||
| - | ------- | + | === Obtenir le chemin du dossier d'un fichier === |
| + | <code python> | ||
| + | import os | ||
| - | ==== Vérifier si un dossier existe ==== | + | filepath = "/ |
| + | os.path.dirname(filepath) # "/ | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Obtenir le dossier courant === | ||
| + | |||
| + | Le dossier d’exécution du script: | ||
| + | <code python> | ||
| + | import os | ||
| + | |||
| + | os.getcwd() | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Obtenir le chemin absolu === | ||
| + | |||
| + | <code python> | ||
| + | import os | ||
| + | os.path.abspath(" | ||
| + | </ | ||
| + | |||
| + | |||
| + | Le dossier du script qui s' | ||
| + | <code python> | ||
| + | import os | ||
| + | os.path.dirname(os.path.abspath(__file__)) | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ==== Vérifier si un dossier | ||
| + | |||
| + | [[https:// | ||
| <code python> | <code python> | ||
| Ligne 20: | Ligne 59: | ||
| # ou | # ou | ||
| os.path.exists(folder) | os.path.exists(folder) | ||
| + | # ou pour les fichiers : | ||
| + | os.path.isfile(filename) | ||
| </ | </ | ||
| - | ------- | + | |
| ==== Créer un dossier ==== | ==== Créer un dossier ==== | ||
| Ligne 34: | Ligne 75: | ||
| - | -------- | + | |
| ==== Lister les fichiers d'un dossier ==== | ==== Lister les fichiers d'un dossier ==== | ||
| Ligne 46: | Ligne 87: | ||
| onlyfiles = [os.path.join(mypath, | onlyfiles = [os.path.join(mypath, | ||
| </ | </ | ||
| + | |||
| + | === Récursivement dans les sous-dossiers === | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <code python> | ||
| + | import glob | ||
| + | |||
| + | print(glob.glob(' | ||
| + | </ | ||
| + | |||
| - | -------- | ||
| ==== Copier un dossier et son contenu ==== | ==== Copier un dossier et son contenu ==== | ||
| Ligne 58: | Ligne 109: | ||
| - | ------ | + | |
| ==== Supprimer un dossier et son contenu ==== | ==== Supprimer un dossier et son contenu ==== | ||
| Ligne 69: | Ligne 120: | ||
| - | ------ | + | |
| ==== Supprimer un fichier ==== | ==== Supprimer un fichier ==== | ||
| Ligne 78: | Ligne 129: | ||
| os.remove(filepath) | os.remove(filepath) | ||
| </ | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ==== Enlever l' | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <code python> | ||
| + | import os | ||
| + | print(os.path.splitext("/ | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Fichiers temporaires ==== | ||
| + | |||
| + | Utilise l' | ||
| + | <code python> | ||
| + | from tempfile import NamedTemporaryFile | ||
| + | |||
| + | f = NamedTemporaryFile(mode=" | ||
| + | f.write(" | ||
| + | f.close() | ||
| + | print(f" | ||
| + | </ | ||
| + | |||