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

finding absolute path

i am trying to understand the question but ididn't get it. please help me out where to start

absolute.py
import os
os.path.isabs("/")

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

I recommend rewatching how to implement functions/methods within Python, but anyways, let's walk you through solving this.

The first instruction we are told to accomplish is: Alright, we'll start off simple. I need you to import the os library.. So let's do just that:

import os

Now for the fun part: Now I need you to write a function named absolute that takes two arguments, a path string and a root string. 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".

It is asking us to create a function called absolute that takes two parameters a path string and a root string:

import os

def absolute(path, root):

It says that we need to check and see if the path is absolute or not, we can check this by using the os method: path.isabs. So let's do that right now!

import os

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

So if it is an absolute path, then we just return the path parameter, otherwise we return the path parameter with the root preppended to it:

import os

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

And that is it we are done!

Again I recommend you go back and watch Python videos on creating functions and conditionals, these are important for later courses, so you need to make sure you understand how and when to use them.