10 May 2012 Supprimer un fichier programmatiquement Fichiers import os # Supprimer le fichier texte 'test.txt' os.remove("c:/test.txt")import os # Supprimer le fichier texte 'test.txt' os.remove("c:/test.txt") python
9 May 2012 Gestion des exceptions pendant l’utilisation d’un fichier Fichiers try: fi = open("c:/here2.mp3", "rb", 0) try:try: fi = open("c:/here2.mp3", "rb", 0) try: python
8 May 2012 Accéder aux propriétés d’un fichier via un objet Fichiers f = open("c:/test.txt", "rb") # Afficher le nom print f.name # Afficher le mode print f.m f = open("c:/test.txt", "rb") # Afficher le nom print f.name # Afficher le mode print f.m python
2 May 2012 Créer et supprimer un répertoire avec les méthodes ‘mkdir()’ et ‘rmdir()’ Fichiers import os # Création du répertoire 'sakobaTest' os.mkdir("c:/sakobaTest") # Suppression d import os # Création du répertoire 'sakobaTest' os.mkdir("c:/sakobaTest") # Suppression d python
1 May 2012 Obtenir la position courante du fichier Fichiers # Ouvrir le fichier en mode Lecture f = open(r"c:/test.txt") #Obtenir la position courante print f# Ouvrir le fichier en mode Lecture f = open(r"c:/test.txt") #Obtenir la position courante print f python
28 April 2012 Modifier la position courante d’un fichier avec le module ‘f.seek(offset [,where])’ Fichiers file = open('c:/test.txt',"w") # Obtenir la position cournate print file.tell() # Modifie file = open('c:/test.txt',"w") # Obtenir la position cournate print file.tell() # Modifie python
26 April 2012 Enregistrer des objets en tant que fichiers(pickling ou sérialisation) Fichiers import cPickle fichier=open("c:/site.obj", "w") site=('python.mesexemples.com', 'cppimport cPickle fichier=open("c:/site.obj", "w") site=('python.mesexemples.com', 'cpp python
24 April 2012 Fonction de découpage des chemins des fichiers Fichiers import os, time path= 'c:/test.txt' print os.path.basename(path) #test.txt print o import os, time path= 'c:/test.txt' print os.path.basename(path) #test.txt print o python
17 April 2012 Parcourir les ligne d’un fichier à l’aide d’une boucle Fichiers f=open('c:/test.txt', 'r') for line in f: print line, f.close()f=open('c:/test.txt', 'r') for line in f: print line, f.close() python
17 April 2012 Fichier/Répertoire: Analyser un chemin Fichiers import os # Vérifier si le chemin existe print os.path.exists('c:/test.txt') # Vérifierimport os # Vérifier si le chemin existe print os.path.exists('c:/test.txt') # Vérifier python