Reputation: 1303
I want to os.path.join
a directory with a file name. The file it's binary and has no extension.
The result is always like:
'C:\\directory\\filename\n'
What I want is of course:
'C:\\directory\\filename'
Without the the last backslash and the n
, i.e. \n
.
My code is:
self.filePath = os.path.join(self.cwd, self.values[index])
How can I get the desired result?
Upvotes: 0
Views: 166
Reputation: 13661
An efficient way which is os independent is to use os.sep
like below:
import os
filepath = os.path.join("C:", os.sep, "directory", "filename")
print(filepath)
Output
C:\directory\filename
Upvotes: 0
Reputation: 5088
s = 'C:\\directory\\filename\n'
s2 = s[:-1]
print(s2)
leads to:
'C:\\directory\\filename'
Upvotes: 1