récupérer le chemin du fichier interprété ? [résolu]

récupérer le chemin du fichier interprété ? [résolu] - Python - Programmation

Marsh Posté le 04-01-2008 à 17:29:58    

Bonsoir !
 
Je bute sur un problème idiot, alors ne vous foutez pas de moi : comment puis-je récupérer le chemin de mon fichier f.py que j'exécute tout bêtement en faisant python f.py ? En cherchant, j'ai appris comment récupérer le chemin de python (sys.executable), mais pas celui de mon fichier !
 
Merci d'avance pour votre aide !


Message édité par suizokukan le 04-01-2008 à 19:20:22

---------------
rule #1 : trust the python
Reply

Marsh Posté le 04-01-2008 à 17:29:58   

Reply

Marsh Posté le 04-01-2008 à 19:04:37    

C'est stocké dans __file__:

$ cat > test.py
print __file__
^C
$ python test.py
test.py


Message édité par masklinn le 04-01-2008 à 19:05:22

---------------
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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 04-01-2008 à 19:20:02    

> Masklinn : comme d'habitude, réponse rapide et précise. Merci !


---------------
rule #1 : trust the python
Reply

Marsh Posté le 04-01-2008 à 20:15:30    

Spoiler :

Je vois le nom du fichier mais pas le chemin. Enfin j'ai pas regardé mais j'imagine que ça n'est pas bcp plus dur.


Et apparemment sys.path pour le chemin.

Message cité 1 fois
Message édité par gzii le 04-01-2008 à 20:21:12
Reply

Marsh Posté le 04-01-2008 à 21:16:50    

gzii a écrit :

Spoiler :

Je vois le nom du fichier mais pas le chemin. Enfin j'ai pas regardé mais j'imagine que ça n'est pas bcp plus dur.


Et apparemment sys.path pour le chemin.


pas trop non.

 

sys.path, c'est le pythonpath (donc la liste de tous les répertoires de la machine dans lesquels python tente de trouver les modules quand on fait un import)


Message édité par masklinn le 04-01-2008 à 21:24:05

---------------
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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 04-01-2008 à 21:33:35    

Je viens de sortir le portable pour tester. Il me semble bien que j'ai le path courant dans sys.path[0]

Reply

Marsh Posté le 04-01-2008 à 21:45:55    

gzii a écrit :

Je viens de sortir le portable pour tester. Il me semble bien que j'ai le path courant dans sys.path[0]


Tu l'as potentiellement, mais pas nécessairement. C'est pas du tout une bonne idée de se reposer sur ça.


---------------
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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 04-01-2008 à 22:14:01    

Ok
os.getcwd() ?

Reply

Marsh Posté le 04-01-2008 à 22:40:13    

ou os.path.abspath().
 
Marche très bien, sauf qu'il ne faut pas changer de changer de cwd


---------------
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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed