473,382 Members | 1,329 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,382 software developers and data experts.

Easier way to get the "here" path?

I have to go into these convulsions to get the directory that the
script is in whenever I need to use relative paths. I was wondering if
you guys have a better way:

from os.path import dirname, realpath, abspath
here = dirname(realpath(abspath(__file__.rstrip("c"))))

In particular, this takes care of the case of symlinked, compiled
scripts, which is fairly common at my workplace, and I imagine in many
*nix evironments.
An illustration:
$echo "print __file__" symlinks/path.py
$ln -s symlinks/path.py
$python
>>import path
path.py
>>reload(path)
path.pyc
<module 'path' from 'path.pyc'>
>>path.__file__
'path.pyc'
>>path.__file__.rstrip("c")
'path.py'
>>from os.path import abspath, realpath
realpath(path.__file__.rstrip("c"))
'/home/bgolemon/python/symlinks/path.py'
>>realpath(abspath(path.__file__.rstrip("c")))
'/home/bgolemon/python/symlinks/symlinks/path.py'
Jul 25 '08 #1
6 4026
bukzor wrote:
I have to go into these convulsions to get the directory that the
script is in whenever I need to use relative paths. I was wondering if
you guys have a better way:
...
If you just need the current path (where it is executed) why not use
os.getcwd()
which returns a string of the absolute path to the module being executed.
$ echo "print __file__" path.py
$ ipython
In [1]: import path
path.pyc

In [2]: import os

In [3]: os.path.join(os.getcwd(), path.__file__.rstrip("c"))
Out[3]: '/home/andrew/path.py'
--
Andrew
Jul 26 '08 #2
bukzor wrote:
>>>from os.path import abspath, realpath
realpath(path.__file__.rstrip("c"))
'/home/bgolemon/python/symlinks/path.py'
>>>realpath(abspath(path.__file__.rstrip("c")))
'/home/bgolemon/python/symlinks/symlinks/path.py'
--
http://mail.python.org/mailman/listinfo/python-list
I find it interesting that I get something different:

In [1]: import path
path.pyc

In [2]: path.__file__
Out[2]: 'path.pyc'

In [3]: path.__file__.rstrip("c")
Out[3]: 'path.py'

In [4]: from os.path import abspath, realpath

In [5]: realpath(path.__file__.rstrip("c"))
Out[5]: '/home/andrew/sym/sym/path.py'

In [6]: realpath(abspath(path.__file__.rstrip("c")))
Out[6]: '/home/andrew/sym/sym/path.py'

I get the same thing for realpath() and realpath(abspath())
It seems to me you can just use:

In [1]: import path
path.pyc

In [2]: from os.path import abspath

In [3]: realpath(path.__file__.rstrip("c"))
Out[3]: '/home/andrew/sym/sym/path.py'

By the way, I am in /home/andrew/sym and path is symlinked from
/home/andrew/sym/sym/path.py to /home/andrew/sym/path.py

--
Andrew
Jul 26 '08 #3
On Jul 25, 10:08*pm, bukzor <workithar...@gmail.comwrote:
I have to go into these convulsions to get the directory that the
script is in whenever I need to use relative paths. I was wondering if
you guys have a better way:

from os.path import dirname, realpath, abspath
here = dirname(realpath(abspath(__file__.rstrip("c"))))

In particular, this takes care of the case of symlinked, compiled
scripts, which is fairly common at my workplace, and I imagine in many
*nix evironments.

An illustration:
$echo "print __file__" symlinks/path.py
$ln -s symlinks/path.py
$python>>import path
path.py
>reload(path)

path.pyc
<module 'path' from 'path.pyc'>>>path.__file__
'path.pyc'
>path.__file__.rstrip("c")
'path.py'
>from os.path import abspath, realpath
realpath(path.__file__.rstrip("c"))

'/home/bgolemon/python/symlinks/path.py'>>realpath(abspath(path.__file__.rstrip("c ")))

'/home/bgolemon/python/symlinks/symlinks/path.py'
How about:

import os
import sys
here = os.path.realpath(os.path.dirname(sys.argv[0]))

It's a little clearer. :-)
Jul 26 '08 #4
Andrew wrote:
bukzor wrote:
>I have to go into these convulsions to get the directory that the
script is in whenever I need to use relative paths. I was wondering if
you guys have a better way:
...
If you just need the current path (where it is executed) why not use
os.getcwd()
which returns a string of the absolute path to the module being executed.
$ echo "print __file__" path.py
$ ipython
In [1]: import path
path.pyc

In [2]: import os

In [3]: os.path.join(os.getcwd(), path.__file__.rstrip("c"))
Out[3]: '/home/andrew/path.py'
--
Andrew
I was thinking of this in the module being imported, but now that I try,
it doesn't work. It is still seeing itself from the symlink's position.

This works, in the module being imported:
$ less path.py
from os.path import realpath
here = realpath(__file__.rstrip("c"))

$ python
>>import path
path.here
'/home/andrew/sym/sym/path.py'

--
Andrew

Jul 26 '08 #5
On Jul 26, 9:19*am, Andrew <alif...@gmail.comwrote:
bukzor wrote:
>>from os.path import abspath, realpath
realpath(path.__file__.rstrip("c"))
'/home/bgolemon/python/symlinks/path.py'
>>realpath(abspath(path.__file__.rstrip("c")))
'/home/bgolemon/python/symlinks/symlinks/path.py'
--
http://mail.python.org/mailman/listinfo/python-list

I find it interesting that I get something different:

In [1]: import path
path.pyc

In [2]: path.__file__
Out[2]: 'path.pyc'

In [3]: path.__file__.rstrip("c")
Out[3]: 'path.py'

In [4]: from os.path import abspath, realpath

In [5]: realpath(path.__file__.rstrip("c"))
Out[5]: '/home/andrew/sym/sym/path.py'

In [6]: realpath(abspath(path.__file__.rstrip("c")))
Out[6]: '/home/andrew/sym/sym/path.py'

I get the same thing for realpath() and realpath(abspath())
It seems to me you can just use:

In [1]: import path
path.pyc

In [2]: from os.path import abspath

In [3]: realpath(path.__file__.rstrip("c"))
Out[3]: '/home/andrew/sym/sym/path.py'

By the way, I am in /home/andrew/sym and path is symlinked from
/home/andrew/sym/sym/path.py to /home/andrew/sym/path.py

--
Andrew
As you can see above, I get the wrong think if I do that. It's very
strange that we're getting different things. I'm using python 2.4,
maybe it was updated in 2.5?
Jul 28 '08 #6
On Jul 28, 10:34*am, bukzor <workithar...@gmail.comwrote:
On Jul 26, 9:19*am, Andrew <alif...@gmail.comwrote:
bukzor wrote:
>>>from os.path import abspath, realpath
>>>realpath(path.__file__.rstrip("c"))
'/home/bgolemon/python/symlinks/path.py'
>>>realpath(abspath(path.__file__.rstrip("c")))
'/home/bgolemon/python/symlinks/symlinks/path.py'
--
>http://mail.python.org/mailman/listinfo/python-list
I find it interesting that I get something different:
In [1]: import path
path.pyc
In [2]: path.__file__
Out[2]: 'path.pyc'
In [3]: path.__file__.rstrip("c")
Out[3]: 'path.py'
In [4]: from os.path import abspath, realpath
In [5]: realpath(path.__file__.rstrip("c"))
Out[5]: '/home/andrew/sym/sym/path.py'
In [6]: realpath(abspath(path.__file__.rstrip("c")))
Out[6]: '/home/andrew/sym/sym/path.py'
I get the same thing for realpath() and realpath(abspath())
It seems to me you can just use:
In [1]: import path
path.pyc
In [2]: from os.path import abspath
In [3]: realpath(path.__file__.rstrip("c"))
Out[3]: '/home/andrew/sym/sym/path.py'
By the way, I am in /home/andrew/sym and path is symlinked from
/home/andrew/sym/sym/path.py to /home/andrew/sym/path.py
--
Andrew

As you can see above, I get the wrong think if I do that. It's very
strange that we're getting different things. I'm using python 2.4,
maybe it was updated in 2.5?
After doing a little testing, it seems that realpath was in fact
improved between 2.4 and 2.5. I need to stick with 2.4, so the
abspath() call is still necessary for me.

Regardless, the answer to my question is that there's no good built-in
way to do this. Even the code above fails for some cases (say the
symlink is named just "c"), making the problem non-trivial. To me,
this is a failure of python's "batteries included". Is this worthy of
a PEP? I'd like to add something like "__absfile__" or maybe
"os.path.absolute_script_path()".

Sample implementation:
def scriptpath():
from inspect import currentframe
file = currentframe().f_back.f_locals.get('__file__', None)
if file.endswith(".pyc"): file = file.rstrip("c")
from os.path import realpath, abspath

return realpath(abspath(file))
Testing:
~/python>ls -l foo.py* c sym/*
lrwxrwxrwx 1 bgolemon asic 10 Jul 28 10:46 c -sym/
bar.py*
lrwxrwxrwx 1 bgolemon asic 10 Jul 28 10:38 foo.py -sym/
bar.py*
-rw-rw-r-- 1 bgolemon asic 149 Jul 28 10:57 foo.pyc
-rwxrwx--- 1 bgolemon asic 76 Jul 28 10:53 sym/bar.py*
-rw-rw-r-- 1 bgolemon asic 149 Jul 28 10:58 sym/bar.pyc

~/python>cat sym/bar.py
#!/usr/bin/env python

from scriptpath import scriptpath
print scriptpath()
~/python>./c
/home/bgolemon/python/sym/bar.py

~/python>./sym/bar.py
/home/bgolemon/python/sym/bar.py

~/python>./foo.py
/home/bgolemon/python/sym/bar.py

~/python>python
>>import foo
/home/bgolemon/python/sym/bar.py

~/python>cd sym/
~/python/sym>python
>>import bar
/home/bgolemon/python/sym/bar.py
--Buck
Jul 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Jim Hill | last post by:
I've done some Googling around on this and it seems like creating a here document is a bit tricky with Python. Trivial via triple-quoted strings if there's no need for variable interpolation but...
0
by: Tom Dacon | last post by:
"Open .Net Command Window Here" context menu for Windows Explorer: The reg file described below adds a new menu item to Windows Explorer's context menu when you right-click over a folder (or the...
5
by: RodneyDunes | last post by:
My site did validate and now it doesn't. The error I get is the following: document type does not allow element "META" here ....nt-type" content="text/html;charset=iso-8859-1"> Can someone...
27
by: Tom Rodman | last post by:
I want to embed a bash script inside the c program. How could you do the below bash snippet in "c"?: cat > /tmp/foo <<\__EOD_ all kinds of nasty stuff in here including single and double...
1
by: JohnZing | last post by:
i tried to install asp.net forums all went ok, but after login i always get "Object moved to here." in firefox or "The page cannot be displayed" in IE when i hit refresh, the page loads fine. ...
1
by: ken.carlino | last post by:
Hi, I have the following compile error instantiated from here, I appreciate if someone can help me find out why? g++ -O0 -g3 -Wall -c -fmessage-length=0 -ostddev.o ../stddev.cpp...
2
by: samyuktha takkellapati | last post by:
create or replace trigger emp_trigger after update or delete on employee for each row begin if updating then insert into empupdate(empno,ename,job,mgr,sysdate,sal,comm,deptno)...
2
by: bhavanirayala | last post by:
Hi All, I am getting the following warning while running the perl script Warning is: Quantifier follows nothing before HERE mark in regex m/* << HERE /
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.