By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi all,
I guess this question was asked many times before, but I don't know
keywords for web search.

Thank you in advance, D.

Jun 7 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Jun 7, 8:39 am, dmitrey <open...@ukr.netwrote:
Hi all,
I guess this question was asked many times before, but I don't know
keywords for web search.

Thank you in advance, D.
import os

d1 = os.path.dirname(__file__)
d2 = os.path.dirname(os.__file__)

print d1
print d2

Jun 7 '07 #2

P: n/a
On Jun 7, 10:18 am, Gerard Flanagan <grflana...@yahoo.co.ukwrote:
On Jun 7, 8:39 am, dmitrey <open...@ukr.netwrote:
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.
>
Hi all,
I guess this question was asked many times before, but I don't know
keywords for web search.
Thank you in advance, D.

import os

d1 = os.path.dirname(__file__)
d2 = os.path.dirname(os.__file__)

print d1
print d2

Jun 7 '07 #3

P: n/a
On Jun 7, 1:19 pm, dmitrey <open...@ukr.netwrote:
On Jun 7, 10:18 am, Gerard Flanagan <grflana...@yahoo.co.ukwrote:On Jun 7, 8:39 am, dmitrey <open...@ukr.netwrote:

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

Jun 7 '07 #4

P: n/a
Gerard Flanagan wrote:
On Jun 7, 8:39 am, dmitrey <open...@ukr.netwrote:
>Hi all,
I guess this question was asked many times before, but I don't know
keywords for web search.

Thank you in advance, D.

import os

d1 = os.path.dirname(__file__)
here I get an error "__file__" is not defined ??
d2 = os.path.dirname(os.__file__)
here I get a completely different path ??
>
print d1
print d2
This seems to work (but I doubt it's always working !! )
print os.getcwd()

so what's the real answer ?

thanks,
Stef Mientki

Jun 7 '07 #5

P: n/a
On Jun 8, 1:01 am, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
Gerard Flanagan wrote:
On Jun 7, 8:39 am, dmitrey <open...@ukr.netwrote:
Hi all,
I guess this question was asked many times before, but I don't know
keywords for web search.
Thank you in advance, D.
import os
d1 = os.path.dirname(__file__)

here I get an error "__file__" is not defined ??
The code must be run from a script or module.
If you run it from an interactive prompt, then you will get a
NameError.
d2 = os.path.dirname(os.__file__)

here I get a completely different path ??
Is it the path of the os module? You can expect pure Python modules to
have a __file__ attribute, but not every module has one - see the
docs.
>
print d1
print d2

This seems to work (but I doubt it's always working !! )
print os.getcwd()

so what's the real answer ?

thanks,
Stef Mientki

Jun 8 '07 #6

P: n/a
dmitrey wrote:
Hi all,
I guess this question was asked many times before, but I don't know
keywords for web search.

Thank you in advance, D.
In the future, please ask your question in the body of your message
(not just in the subject line).

This question has no answer in general. There are some things like
__file__ that may be "good enough" depending on your usage, but keep
in mind in general that:
1. __file__ may be altered directly by the program and give an answer
that has no past or current relationship to any filename for the file
that contains the code currently being run.
2. Even if you make no changes, it can contain arbitrary results in
not uncommon situations (e.g. in my local install, an interactive
python session--or one from a here-document in the shell--lists
~/.pythonrc or "<stdin>" in __file__, even though in neither case was
I running my .pythonrc or a file named "<stdin>"
3. Without that, any file may have 0, 1, or many names; those names
may differ now from what they were when the file was first accessed.
So __file__ may refer to an unlinked name (even in the case where at
least one name that existed when you first executed the file is still
valid), or may even e the name of another, different file that was
created (or linked/renamed) after you started running things.

Depending on your needs, those factors may or may not matter. They're
certainly worth being aware of, and for some applications they may
have massive security implications.

Jun 8 '07 #7

P: n/a
On 7 , 10:39, dmitrey <open...@ukr.netwrote:
Hi all,
I guess this question was asked many times before, but I don't know
keywords for web search.

Thank you in advance, D.
import sys
file_name = sys.argv[0]

????????????
this?

Jun 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.