Je cherche à savoir dans quel répertoire est installé mon script python.
J'ai cherché dans les modules os et sys mais sans succès.
Pour être clair :
J'ai un script python /home/toto/bin/script.py
Je voudrais récupérer /home/toto/bin.
Or avec os.getcwd(), je récupère le répertoire dans lequel je suis
quand je lance le script (ie si je suis dans /home/toto et que je tape
bin/script.py, os.getcwd() me reverra/home/toto).
Je peux bidouiller avec sys.argv[0] et os.getcwd() mais je pense qu'il y a
quelque chose de beaucoup plus simple que je n'ai pas vu ?
Le Mon, 19 Sep 2005 20:26:27 +0200, Bruno Desthuilliers a écrit :
Bonjour,
Je cherche à savoir dans quel répertoire est installé mon script python.
bruno $ cat playground/whereami.py import os, os.path print os.path.dirname(os.path.join(os.getcwd(), __file__))
bruno $ python playground/whereami.py /home/bruno/playground ---
Oui, c'est ça. Il y a juste un problème quand on lance le script avec ./whereami.py quand on est dans le répertoire playground. On obtient alors /home/bruno/playground/. mais c'est facile à gérer. Ta méthode revient exactement à la mienne, je voulais juste être sûr qu'il n'y ait pas plus clean.
Merci !
-- NSZ
Le Mon, 19 Sep 2005 20:26:27 +0200, Bruno Desthuilliers a écrit :
Bonjour,
Je cherche à savoir dans quel répertoire est installé mon script python.
bruno@bibi bruno $ python playground/whereami.py
/home/bruno/playground
---
Oui, c'est ça. Il y a juste un problème quand on lance le script avec
./whereami.py quand on est dans le répertoire playground. On obtient
alors /home/bruno/playground/. mais c'est facile à gérer. Ta méthode
revient exactement à la mienne, je voulais juste être sûr qu'il n'y ait
pas plus clean.
Le Mon, 19 Sep 2005 20:26:27 +0200, Bruno Desthuilliers a écrit :
Bonjour,
Je cherche à savoir dans quel répertoire est installé mon script python.
bruno $ cat playground/whereami.py import os, os.path print os.path.dirname(os.path.join(os.getcwd(), __file__))
bruno $ python playground/whereami.py /home/bruno/playground ---
Oui, c'est ça. Il y a juste un problème quand on lance le script avec ./whereami.py quand on est dans le répertoire playground. On obtient alors /home/bruno/playground/. mais c'est facile à gérer. Ta méthode revient exactement à la mienne, je voulais juste être sûr qu'il n'y ait pas plus clean.
Merci !
-- NSZ
NonSenZ
Le Mon, 19 Sep 2005 22:23:22 +0200, Do Re Mi chel La Si Do a écrit :
Bonsoir !
Ou, plus simplement : os.path.dirname(__file__) ---
Nope, ne renvoie pas le chemin absolu du script. Par exemple si on le lance depuis le répertoire en tapant ./script.py, os.path.dirname(__file__) renvoie .
-- NSZ
Le Mon, 19 Sep 2005 22:23:22 +0200, Do Re Mi chel La Si Do a écrit :
Bonsoir !
Ou, plus simplement : os.path.dirname(__file__)
---
Nope, ne renvoie pas le chemin absolu du script. Par exemple si on le
lance depuis le répertoire en tapant ./script.py,
os.path.dirname(__file__) renvoie .
Le Mon, 19 Sep 2005 22:23:22 +0200, Do Re Mi chel La Si Do a écrit :
Bonsoir !
Ou, plus simplement : os.path.dirname(__file__) ---
Nope, ne renvoie pas le chemin absolu du script. Par exemple si on le lance depuis le répertoire en tapant ./script.py, os.path.dirname(__file__) renvoie .
-- NSZ
NonSenZ
Le Mon, 19 Sep 2005 23:36:57 +0200, Bruno Desthuilliers a écrit :
Je voulais bien sûr dire: print os.path.dirname(os.path.realpath(__file__)) ---
Cette fois c'est parfait.
Merci beaucoup.
-- NSZ
Le Mon, 19 Sep 2005 23:36:57 +0200, Bruno Desthuilliers a écrit :
Je voulais bien sûr dire:
print os.path.dirname(os.path.realpath(__file__))
---