Jeff
Jeff

Reputation: 4433

Python relative path problems

I am trying to create some helper functions that will give me a folder based on the relative paths:

def get_BASE_dir_path():
    cur_dir = os.path.dirname(os.path.abspath(__file__))
    BASE  = os.path.abspath(os.path.join(cur_dir,"..",".."))
    return BASE

def get_src_dir_path():
    BASE = get_BASE_dir_path()
    src_dir  = os.path.abspath(os.path.join(BASE,"src"))
    return src_dir

def get_lib_dir_path():
    BASE = get_BASE_dir_path()
    lib_dir = os.path.dirname(os.path.join(BASE,"src","lib"))
    return lib_dir

def get_ffmpeg_dir_path():
    BASE = get_BASE_dir_path()
    ffmpeg_dir= os.path.dirname(os.path.join(BASE,"src","lib","ffmpeg"))
    return ffmpeg_dir

But, somehow, I am not getting the right results when I print the functions:

Output:

C:\dev\project
C:\dev\project\src
C:\dev\project\src
C:\dev\project\src\lib

What did I miss?

Upvotes: 2

Views: 252

Answers (2)

Hai Vu
Hai Vu

Reputation: 40773

The problem is here, in function get_lib_dir_path()

lib_dir = os.path.dirname(os.path.join(BASE,"src","lib"))

It should be:

lib_dir = os.path.join(BASE,"src","lib")

The same thing happens in get_ffmpeg_dir_path(). By calling dirname(), you chop off the last directory.

Upvotes: 1

haraprasadj
haraprasadj

Reputation: 1087

I guess it is because you are returning dirname instead of abspath for the last two values.

Upvotes: 1

Related Questions