2

I have the following string containing absolute directory of a file.

'D:\Sample\Project\testXcl\data.xlsx'

On passing this into os.path.abspath, I am getting the following result:

'D:\\Sample\\Project\testXcl\\data.xlsx'

This happens because TestXcl folder name is read as \t. Wrong path/error also appears if any file/folder name is starting with n, a, b, f, r, v, x.

Is there any other method to rectify this, or should I go about replacing the string with correct file delimiters ?

Sumit Bisht
  • 1,507
  • 1
  • 16
  • 31

2 Answers2

6

When you specify the path name, either escape the backslashes or use a raw string literal:

p = 'D:\\Sample\\Project\\testXcl\\data.xlsx'
p = r'D:\Sample\Project\testXcl\data.xlsx'
phihag
  • 278,196
  • 72
  • 453
  • 469
5

Use a raw string literal instead.

filename = r'D:\Sample\Project\testXcl\data.xlsx'
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358