relative paths connect using python

Steve D'Aprano steve+python at
Mon Mar 20 13:37:09 EDT 2017

On Tue, 21 Mar 2017 04:05 am, Xristos Xristoou wrote:

> i have a little confused problem. i want to store some paths from images
> using python 2.7 in windows 10. i have some relative path like this
> var1='C:/my/store/path' 

That's not a relative path, that's an absolute path.

> and in the final folder where in my example is the 
> name 'path' inside that folder i have some images like this : -path
>    -myimage_1010_im.png
>    -myimage_1010_im1.png
>    -myimage_1010_im3.png
>    -myimage_1020_im.png
>    -myimage_1020_im1.png
> can i connect my relative path like var1 

var1 is not a relative path. It is an absolute path. Absolute paths start
with a drive letter like C. Relative paths have no drive letter and start
in the current working directory.

> with the images using only the 
> number of the mid name of images and finaly i take the full original path
> like this var1='C:/my/store/path/myimage_1010_im.png' ? 

I am afraid I have no idea what your code is doing. Instead of describing
what your Python code isn't doing, can you show the code so we can see what
it is doing?

And please use variable names that mean something.

# terrible variable names
var, var1, var2

# good variable names
directory_name, full_path, filename

> i try something 
> but not work import os cwd = os.getcwd()
> path = os.path.join(cwd, "my_file")

That works perfectly for me:

py> import os
py> cwd = os.getcwd()
py> path = os.path.join(cwd, "my_file")
py> print path

What were you expecting it to do? Can you tell us what result you expected,
and what result you got?

“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

More information about the Python-list mailing list