philippos
philippos

Reputation: 1303

Joining a directory name with a binary file name

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

Answers (2)

arshovon
arshovon

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

mrCarnivore
mrCarnivore

Reputation: 5088

s = 'C:\\directory\\filename\n'
s2 = s[:-1]
print(s2)

leads to:

'C:\\directory\\filename'

Upvotes: 1

Related Questions