Python Forum
automatically get absolute paths
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
automatically get absolute paths
#1
Hello,
I want to automatically convert relative paths to absolute paths.


dirname = os.path.dirname(__file__)
os.path.join(dirname, r'relative/path/file.txt')

This worked fine but I used this in several places so I thought it makes sense to put it in a separate module. So I did. But when I import it, __file__ points to the module, not to the calling file.

I could give __file__ as an argument enverytime I call that function but it would be easier if I don't have to do that. Any ideas?

Thats the (very simple) function:
import os def AbsPath(relPath): dirName = os.path.dirname(__file__) absPath = os.path.join(dirName, relPath) return absPath
Reply
#2
I use a trick similar to this in small programs that use an image or sound file. Get the path to the local file and use that as the base for the files you want to open. I'm pretty sure there is no way to make it work the same way as an imported module. It's a bit of a hack anyway.

That doesn't mean it isn't useful at all. If you put your files in the same folder as this module it will work. For example, you have a folder containing all your image files and you want a simple way to get an image file. Write a version of this named imagefiles.py.
import os DIRECTORY = os.path.dirname(__file__) def get(relPath): return os.path.join(DIRECTORY, relPath)
From your program that uses image files.
import imagefile abs_path = image_file.get('ship.png')
Reply
#3
After 5 years, it's as simple now:

import os abs_path = os.path.abspath(relpath)
[/quote]
Reply
#4
(Aug-12-2025, 05:49 AM)rundekugel Wrote: After 5 years, it's as simple now:
Better use pathlib
from pathlib import Path abs_path = Path(relpath).resolve()
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 5,753 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  pdf2image, poppler and paths jehoshua 18 38,174 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 2,932 May-29-2022, 09:11 AM
Last Post: snippsat
  Absolute beginner am I missing something? kamren 7 5,480 Sep-25-2020, 05:32 AM
Last Post: buran
  chkFile with absolute paths JarredAwesome 7 5,235 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  absolute imports between folders mikisDW 0 2,367 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  Paths millpond 12 10,106 Jul-30-2020, 01:16 PM
Last Post: snippsat
  Problems with windows paths delphinis 6 13,749 Jul-21-2020, 06:11 PM
Last Post: Gribouillis
  I am an absolute newbie.Help me! feynarun 4 4,006 Apr-11-2020, 09:25 PM
Last Post: feynarun
  Shortest paths to win snake and ladder sandaab 5 6,115 Jun-30-2019, 03:20 PM
Last Post: sandaab

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.