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 Project Starter Solid Paths

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

Why do I have to convert my path variable to a string to convert a relative path to an absolute one? (Using Python 3.)

In Python for Filesystems, in the Solid Paths, challenge, I was able to get this function to pass but only if I changed the root variable to a string.

Why?

def get_root():
    root = pathlib.PurePath(
        input("What's the full path where you'd like the project? ")
    )
    if not root.is_absolute():
        root = os.path.abspath(str(root))
    return root

However, if I test this on my Mac, without the conversion to an string, it works just fine.

Which is correct/best?

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

You probably have Python 3.6 installed on your system (which is a good thing!), but Treehouse uses Python 3.5 (at least in Workspaces, but I'd wager a guess that applies to challenges, too). In previous versions of Python, functions in the os.path module only accepted strings or bytes as parameters, but one of the changes in Python 3.6 is that these functions were updated to also accept objects from the pathlib module, like PurePath. Until Treehouse updates their version of Python, you must convert your pathlib objects to a string before passing them to an os.path function, but in writing real code going forward, it's better to just use your pathlib objects directly