Reputation: 231
I am trying to append File separator at the end of a given directory path. But os.path.join
is not appending the required separator.
I have tried the below code. Consider directoryPath = //TEAM/PATH_1/PATH_2/2018-Aug-06
os.path.join(directoryPath, "")
But the output it gives is
//TEAM/PATH_1/PATH_2/2018-Aug-06\
So i tried the below code.
if(len(directoryPath.split("/")) >= 1):
return os.path.join(directoryPath, "/")
else:
return os.path.join(directoryPath, "\\")
For this output was
//TEAM/PATH_1/
Can someone guide me the correct way to append File Separator at the end.
Upvotes: 0
Views: 1907
Reputation: 13176
os.path.join automatically use the correspondence OS path separator.
If you intend to run your command in windows but want the /
slash separator, just just replace
. i.e.
os.path.join(directoryPath, '').replace('\\', '/')
Upvotes: 1
Reputation: 5741
Why not just add a a slash at the end of your string?
>>> directoryPath = '//TEAM/PATH_1/PATH_2/2018-Aug-06'
>>> directoryPath += '/'
>>> directoryPath
'//TEAM/PATH_1/PATH_2/2018-Aug-06/'
Upvotes: 0
Reputation:
os.path.join
will use the separator for the OS you run the program on (accessible via os.sep
variable).
If what you want is to reuse the separator from existing variable (directoryPath
) independently of OS your program is running on, then you should not rely on os
package at all - just append the string to the path.
Upvotes: 0