Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Stephen ColeCourses Plus Student 15,592 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.
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?
Michael Hulet47,893 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