Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python for File Systems Navigation Absolutely

Unknown issues with joining & absolute paths (Python for File Systems, Navigation, Absolutely, Challenge Task 2)

The requirements are to define a function absolute that takes a path string and a root string, checks if the path is absolute, returns the path if it is, or returns the joined root and path if it isn't. My code (attached) should do that, but when I submit on Treehouse I get:

Bummer: Didn't get the right return values for some paths

On my machine (Linux, Python 3.6), it appears to work as required:

>>> import os
>>> def absolute(path, root):
...     return path if os.path.isabs(path) else os.path.join(root, path)
...
>>> absolute("projects/python_basics/", "/")
'/projects/python_basics/'
>>> absolute("/home/kenneth/django", "C:\\")
'/home/kenneth/django'
>>> absolute("projects/python_basics/", "/home/kenneth")
'/home/kenneth/projects/python_basics/'
>>> absolute("/projects/python_basics/", "/home/kenneth")
'/projects/python_basics/'

I am at a loss. Any insight? Is this an os/platform difference issue?

absolute.py
import os

def absolute(path, root):
    return path if os.path.isabs(path) else os.path.join(root, path)

3 Answers

Well, it passed with:

absolute.py
import os

def absolute(path, root):
    return path if os.path.isabs(path) else ''.join((root, path))

Sure. Whatever. Go ahead and use simple concatenation on paths.

Steven Parker
Steven Parker
229,732 Points

On a hunch, I tried replacing the "join" method call with a simple concatenation of the two arguments.

And it passed.

So try that, but you may also wish to send a bug report to the Support folks.

I'm also going to tag Craig Dennis in hopes that he can explain why it doesn't work .

CHRISTOPHER HECKER
CHRISTOPHER HECKER
1,865 Points

This issue persists, Sept 2018

Hey am not sure if it is a bug. I was getting the same error and was thinking the same is it a bug.

I passed the challenge with the below code.

import os

def absolute(path, root):
    if os.path.isabs(path) == True:
        path_1 = str(path)
        return path_1
    else:
        path_2 = str(root + path)
        return path_2

path_1 = str(absolute("projects/python_basics/", "/"))
path_2 = str(absolute("/home/kenneth/django", "C:\\"))

print(path_1)
print(path_2)
print(type(path_1))
print(type(path_2))

As the join method returns a string I can see why this passed it might be on the return that the variable is not a string type as expected.

I could be totally wrong but I like to make sure by using a method on the calling the function so make sure am returning the correct variable type.