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 trialRod Castillo
5,122 PointsCode challenge - Python for Filesystems
Hey All,
I believe that I am close to solving the code challenge, but keep getting an error.
It works in my local environment. Any assistance would be greatly appreciated,
Rod
def absolute (path, root):
# If the path is not already absolute, return the path with the root prepended to it.
# For example: absolute("projects/python_basics/", "/") would
# return "/projects/python_basics/" while
# absolute("/home/kenneth/django", "C:\") would return "/home/kenneth/django".
# Check to see if the path begins with the same character as the absolute
# If no, then pre-apprend the path with the absolute value
if (path.startswith(root)):
return path
elif (root == "C:\\"):
return path
else:
path = root + path
return path
Rod Castillo
5,122 Points1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsRod,
This question is a little strange and I got stuck on it too. My solution is classical python interpretation-- NOTE THIS DOES NOT WORK (but should). I interpret absolute path to be a path that begins with a "slash" or drive letter (e.g. C:\ for Windows systems). To me, if you're going to import "os" library, you should take advantage of its features os.path.isabs() and os.path.join()
import os
def absolute(path, root):
# check if path_str is absolute
if os.path.isabs(path):
return path
# return joined path
return os.path.join(root, path)
But a Treehouse insider gave me a hint that will help you pass it. At some point, I will see if Treehouse (or maybe I can volunteer) to rewrite the question wording and tests!!
Note that the code below passes, but doesn't actually conform to the description. And also doesn't use the "os" library which was imported. :-(
TREEHOUSE HINT: if '/home' is not in the path string, return the conjoined root and path.
--SPOILER BELOW--
import os
def absolute(path, root):
if '/home' not in path:
return root + path
else:
return path
Josh Keenan
20,315 PointsJosh Keenan
20,315 Pointscan you link to the challenge?