stack twitter rss linkedin cross

Wilco van Esch

Skip to main content

Search results

    Combining multiple path sections with os.path.join()

    Assume these variables are supplied:

    base_path = "C:\\Python34\\myscript\\"
    filename = "sassykitten.jpg"
    

    This won't work:

    path = os.path.join(base_path, "/images", filename)

    The variable path will now contain:

    "C:/images\\sassykitten.jpg"
    

    This is due to the slash in /images, which will make the rest of the join start at root. You can join any number of strings, but they should not include slashes.

    Do this instead:

    path = os.path.join(base_path, "images", filename)

    You will get the desired:

    "C:\\Python34\\myscript\\images\\sassykitten.jpg"