Rechercher un fichier dans une arborescence
Bonjour,
J'ai un problème concernant la récupération d'un fichier dans une arborescence, je m'explique, j'ai mon arborescence de la façon suivante:
home > work > tech > tech1,tech2,tech3 et dans chaque répertoire (tech1,tech2 ou tech3) j'ai un fichier tools1.txt pour tech1, tools2.txt pour tech2 et ainsi de suite...
L'idée est de récupérer le bon fichier en fonction que je choisisse de travailler avec tech1, tech2 ou tech3......J'ai fait le programme suivant:
Code:
1 2 3 4
| def Techno():
import glob
f = glob.glob("/home/work/tech/%(choix)s/*.txt")
result = open(f % {'choix' : "tech1"}, mode = "r") |
l'idée étant de choisir le fichier à ouvrir en fonction du répertoire dans lequel on veut travailler......mon programme ne marche marche pas sa m'affiche "unsupported operand type(s) for %: 'list' and 'dict'.....s'y vous pouviez m'aidez svp
merci d'avance
oops, get_name serait mieux avec return
Ben maintenant on a des wildcards dans le nom, il faut remettre le glob.glob pour récupérer un "nom" qui existe.
Genre:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
import glob
LAB_PATTERN = "/home/work/techno/01/%(techno)s/LAB/%(techno)s_*.lab"
LIK_PATTERN = "/home/work/techno/01/%(techno)s/LIK/%(techno)s_*.lik"
FAG_PATTERN = "/home/work/techno/01/%(techno)s/FAG/%(techno)s_*.fag"
def get_name(pattern, techno):
files = glob.glob(pattern % vars())
assert len(files) is 1, "si >1 ou zero: revoir l'algo"
return files[0]
# usage
techno = "XXXX"
lab_file = get_name(LAB_PATTERN, techno) |
- W