Comments on: Chemin absolu et dossier du script en bash ou Python http://sametmax.com/chemin-absolu-et-dossier-du-script-en-bash-ou-python/ Du code, du cul Fri, 06 Sep 2019 09:34:15 +0000 hourly 1 https://wordpress.org/?v=4.9.7 By: jean http://sametmax.com/chemin-absolu-et-dossier-du-script-en-bash-ou-python/#comment-20299 Mon, 24 Feb 2014 14:04:03 +0000 http://sametmax.com/?p=762#comment-20299 Pour le dossier du script en python :

La commande :
import os
dossier = os.path.dirname(os.path.abspath(__file__))

marche lorsqu’on execute le script par le terminal (par exemple avec : python nom_script.py) mais elle ne marche pas (en tout cas, pas chez moi) si on execute avec idle (là c’est os.getcwd() qui marche).

Bref pour avoir quelquechose qui fonctionne sous idle ET dans un terminal j’utilise (ce n’est pas de moi !) :

import os
import inspect
repertoire_script=os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))

]]>
By: Sam http://sametmax.com/chemin-absolu-et-dossier-du-script-en-bash-ou-python/#comment-790 Tue, 24 Jul 2012 19:23:57 +0000 http://sametmax.com/?p=762#comment-790 Pour le mec qui a cherché python repertoire du script, la solution est le dernier bout de code de l’article.

Attention, c’est dossier dans lequel est le script, pas le dossier depuis lequel le script est éxécuté, qui lui se trouve en faisant:

>>> import os
>>> os.getcwd()
'/home/sam'
]]>