masklinn í dag viðrar vel til loftárása | listdir pour le contenu d'un dossier, pas besoin de sortir glob pour un truc pareil izip + chain.from_iterable l'entrelac en python, les chemins ne se join pas à la main, prière d'utiliser os.path.join Code :
import functools, sys, os, itertools path_to = functools.partial(os.path.join, sys.argv[1]) list1 = os.listdir(path_to('imageG')) list2 = os.listdir(path_to('imageB')) assert len(list1) == len(list2) with open(path_to('list.txt'), 'wb') as out: for filename in itertools.chain.from_iterable(itertools.izip(list1, list2)): out.write(filename + '\n')
| Si on veut éviter que ce ne soit lisible, je recommande d'y aller à coup de writelines ou de join par dessus, et en laissant le refcount CPython s'occuper du fichier:
Code :
import functools, sys, os, itertools path_to = functools.partial(os.path.join, sys.argv[1]) list1 = os.listdir(path_to('imageG')) list2 = os.listdir(path_to('imageB')) assert len(list1) == len(list2) open(path_to('list.txt'), 'wb').write( '\n'.join(itertools.chain.from_iterable(itertools.izip(list1, list2))))
| Mais bon, sur ce genre d'utilities j'aurais tendance à sortir directement sur stdout et à piper depuis le shell.
Code :
import functools, sys, os, itertools path_to = functools.partial(os.path.join, sys.argv[1]) list1 = os.listdir(path_to('imageG')) list2 = os.listdir(path_to('imageB')) if len(list1) != len(list2): sys.exit(1) for filename in itertools.chain.from_iterable(itertools.izip(list1, list2)): print(filename)
|
Message édité par masklinn le 01-04-2011 à 22:17:44 ---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
|