howto obtain directory where current (running) py-file is placed?

Gerard Flanagan grflanagan at yahoo.co.uk
Thu Jun 7 14:13:17 CEST 2007


On Jun 7, 1:19 pm, dmitrey <open... at ukr.net> wrote:
> On Jun 7, 10:18 am, Gerard Flanagan <grflana... at yahoo.co.uk> wrote:> On Jun 7, 8:39 am, dmitrey <open... at ukr.net> wrote:
>
> Thank you.
> And what is the simplest way (without split/join, if exist) to obtain
> name of directory parent to directory my_directory_name?
>
> Thx, D.
>

>>> os.path.join(os.pardir, os.path.dirname('/a/b/c/d/e/f.log'))
'/a/b/c/d/e'
>>> os.path.join(os.pardir, os.path.dirname(_))
'/a/b/c/d'
>>> os.path.join(os.pardir, os.path.dirname(_))
'/a/b/c'
>>> os.path.join(os.pardir, os.path.dirname(_))
'/a/b'
>>> os.path.join(os.pardir, os.path.dirname(_))
'/a'
>>> os.path.join(os.pardir, os.path.dirname(_))
'/'
>>> os.path.join(os.pardir, os.path.dirname(_))
'/'

(When using the interactive interpreter, an underscore '_' means 'the
previous result')

HTH

Gerard




More information about the Python-list mailing list