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,497
  • 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
  • 263,143
  • 67
  • 432
  • 458
5

Use a raw string literal instead.

filename = r'D:\Sample\Project\testXcl\data.xlsx'
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325