1

So i'm trying to create path using the code mentioned below:

path = os.path.join(os.path.dirname(__file__),'folder_abc','file.abc')

But it keeps giving the wrong path. i.e for the above statement, value of path variable is set to :

C:/User/abc\folder_abc\file.abc

see, before abc '/' is used and after it '\' . Why is this happening ?

CYAN CEVI
  • 813
  • 1
  • 9
  • 19

2 Answers2

3

Thanks to SSchneid.

using os.path.normpath() solved this.

i.e in above case :

path = os.path.normpath(os.path.join(os.path.dirname(__file__),'folder_abc','file.abc'))
CYAN CEVI
  • 813
  • 1
  • 9
  • 19
1

This is described in the Python docs see here:

https://docs.python.org/2/library/os.path.html#os.path.join

It means, that your operating system separator is set to '\' and not as you would like to '/'. but touching these variables is not recommended as described here in another stackoverflow post:

Python - Can (or should) I change os.path.sep?

Community
  • 1
  • 1
sebisnow
  • 1,671
  • 17
  • 26