473,236 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 software developers and data experts.

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

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
7 1665
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Schwalm | last post by:
I'd like to modify the python search path depending on the source directory of the script being started. The reason is: I use a version control system, and the python scripts and modules are...
6
by: Peter Maas | last post by:
My goal is to have the top level of a directory tree in the Python path without touching anything outside the directory. I tried to create .pth files with the top level path in every subdirectory...
3
by: Jacek Jurkowski | last post by:
Run a Process using current user login? I hava a service that works as LocalSystem and i'm running some Process but I would Like to run it as an current logged user or admin even? How to obtain...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
7
by: patrik.kahari | last post by:
Is there a c++ function similar to getcwd that does not give you the working directory but the directory or path of the currently running executable? I need this because im porting a game that...
1
by: Harlin Seritt | last post by:
I have a script that I want to import called upper.py. It is 2 directories above the script that will call it - i'll call that one main.py. How am I able to import a script in a directory that is...
4
by: brad | last post by:
When I use idle or a shell to execute a python script, the script executes in the directory it is currently in (in this case, my desktop). However, when using GNOME and right clicking the py script...
5
by: David Lees | last post by:
August 31, 2007 I just downloaded the current Cython release and have no problem running the cpython.py translator on the demo code. But when I try compiling, I get an error complaining that my...
1
by: dmitrey | last post by:
howto compile recursively all *.py files to *.pyc (from a directory my_dir)? Thank you in advance, D.
4
by: stef mientki | last post by:
hello, my program has become a bit large, and now I want to split the files over several subdirectories. So in the example shown below, I just moved the files f1.py and f2.py to a deeper...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.