0

I am trying this in Windows 8.1, with python v.3.4. The os.path module has a join method which, according to the documentation, is a safe way to join fragments of files or folders without mixing up back and forward slashes. In the code snippet below I am trying to join a file and a folder:-

>>> photo = r"\camera\picnic.jpg"
>>> folder = os.getcwd()
>>> print(folder)
C:\Users\Renae
>>> path = os.path.join(folder, photo)
>>> print(path)
C:\camera\picnic.jpg

And boom goes the dynamite. I was expecting path to be C:\Users\Renae\camera\picnic.jpg. I've tried removing the r in front of photo with no results. I've also tried backslashes even though Windows uses forward slashes which made it worse. The result was a mix of back and forward slashes. If I remember correctly this was not a problem in Linux.

1 Answer 1

2

Try removing the initial slash.

I can't speak for windows because it's been a long time since working on windows, but in *nix systems, starting a path with a slash signifies the root of the file system. I'm guessing that the implementation in python (and possibly more languages) use this convention on windows as well. I don't have a windows box to verify this on though.

Sign up to request clarification or add additional context in comments.

2 Comments

The reason why I used r is to avoid cases like folder\night\update.dat. Here \n and \u will be wrongly interpreted by python's internal processing.
Ah of course, I forgot you're using backslashes -- my fault. Definitely remove the initial backslash though and you should be good to go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.