无法从python os.path.abspath获取有效的路径名 [英] Unable to get a valid pathname from python os.path.abspath
本文介绍了无法从python os.path.abspath获取有效的路径名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下包含文件绝对目录的字符串.
I have the following string containing absolute directory of a file.
'D:\Sample\Project\testXcl\data.xlsx'
将其传递到os.path.abspath时,我得到以下结果:
On passing this into os.path.abspath, I am getting the following result:
'D:\\Sample\\Project\testXcl\\data.xlsx'
发生这种情况是因为TestXcl文件夹名称被读取为\ t. 如果任何文件/文件夹名称以n,a,b,f,r,v,x开头,也会出现错误的路径/错误.
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 ?
推荐答案
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'
这篇关于无法从python os.path.abspath获取有效的路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文