import os CHAR_ID = "#" CHAR_NEW_LINE = "\n" NOM_FICHIER = "data_base.txt" encodage_en_cours = True my_id = "" prenom = "" nom = "" age = "" def select_dossier(nom_dossier): #si le dossier n'existe pas on le cree if not os.path.isdir(nom_dossier): os.mkdir(nom_dossier) os.chdir(nom_dossier) def demande_si_encodage(): nouvel_encodage = False reponse = raw_input("Si vous voulez faire un encodage, tapez o sinon tapez n : ") if reponse == 'o' or reponse == 'O': nouvel_encodage = True return nouvel_encodage def demande_info(): global my_id, prenom, nom, age my_id = raw_input("Veuillez introduire un ID :") prenom = raw_input("Veuillez introduire votre prenom :") nom = raw_input("Veuillez introduire votre nom :") age = raw_input("Veuillez introduire votre age:") def ajoute_au_fichier(nom_fichier): my_file = open(nom_fichier,"a") my_file.write(CHAR_ID) my_file.write(my_id) my_file.write(CHAR_ID + CHAR_NEW_LINE) my_file.write(prenom + CHAR_NEW_LINE) my_file.write(nom + CHAR_NEW_LINE) my_file.write(age + CHAR_NEW_LINE) my_file.write(CHAR_NEW_LINE) my_file.close() def affiche_fichier(nom_fichier): #on parcourt tout le fichier et on l'affiche my_file = open(nom_fichier,"r") my_record = '' for a_line in my_file: if a_line == CHAR_NEW_LINE: # un "\n" seul à chaque fin de record print my_record #comme print crée un saut de ligne automatique my_record = '' #on ne print qu'une fois par record else: my_record += a_line my_file.close() def affiche_id_fichier(nom_fichier): #on parcourt tout le fichier et on affiche l'id my_file = open(nom_fichier,"r") current_id = '' reading_an_id = False for a_line in my_file: for a_char in a_line: if reading_an_id: if a_char == CHAR_ID: #then finish reading print current_id current_id = '' reading_an_id = False else: current_id += a_char elif a_char == CHAR_ID: #beginning an ID reading reading_an_id = True my_file.close() #début du programme select_dossier("data") while(encodage_en_cours): demande_info() ajoute_au_fichier(NOM_FICHIER) encodage_en_cours = demande_si_encodage() affiche_fichier(NOM_FICHIER) affiche_id_fichier(NOM_FICHIER)