0

I am having a problem with a simple piece of code, which run on both OSX and Windows. I am using Python 3.7.9.

All I am doing is to chain a path to a filename; I read a directory content, then create the full path to open this file

The path is relative; I run the python script in the same directory where images_to_rename is located

from os import path, listdir

# Dir where the images are
assets = "images_to_rename"
# create a list of files in that dir
filelist = listdir(assets)

for file in filelist:
    # join path and file name
    joined_name = path.join(assets, file)
    print(joined_name)

let's say that the folder has panorama.png and house.png. When I print on Windows, I get images_to_rename\\panorama.png, which is wrong, since it should be images_to_rename\panorama.png. Not sure what is the point to use path.join if it cannot correctly use the same code on win and OSX.

I can work around by replacing \\ with \ with a replace when I call path.join, but I was hoping to avoid that using path join. Am I missing something?

  • the path is correct in both windows and Mac, it is just the way it is shown, in windows it is shown as \\ – Reza Sep 08 '20 at 07:04
  • The \\ in the printed value is just Python’s representation of a \ - i.e. there’s nothing wrong. And presumably your code finds the file which is the best confirmation of that. – DisappointedByUnaccountableMod Sep 08 '20 at 07:05
  • I don't see the same behaviour with Python 3.6 on Windows 10. – Selcuk Sep 08 '20 at 07:08
  • 1
    Thanks; so fundamentally it does not matter if there are 2 `//` it does not mater much. I stopped there and didn't execute the rest of the code to open the file when I saw that, but indeed the load code works. Thanks –  Sep 08 '20 at 07:16

0 Answers0