Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从python os.path.abspath获取有效的路径名_Python_Windows_Path - Fatal编程技术网

无法从python os.path.abspath获取有效的路径名

无法从python os.path.abspath获取有效的路径名,python,windows,path,Python,Windows,Path,下面的字符串包含文件的绝对目录 'D:\Sample\Project\testXcl\data.xlsx' 在将其传递到os.path.abspath时,我得到以下结果: 'D:\\Sample\\Project\testXcl\\data.xlsx' 这是因为TestXcl文件夹名称被读取为\t。 如果任何文件/文件夹名称以n、a、b、f、r、v、x开头,也会出现错误路径/错误 是否有其他方法来纠正此问题,或者我是否应该用正确的文件分隔符替换字符串?使用原始字符串文字 filename =

下面的字符串包含文件的绝对目录

'D:\Sample\Project\testXcl\data.xlsx'
在将其传递到os.path.abspath时,我得到以下结果:

'D:\\Sample\\Project\testXcl\\data.xlsx'
这是因为TestXcl文件夹名称被读取为\t。 如果任何文件/文件夹名称以n、a、b、f、r、v、x开头,也会出现错误路径/错误


是否有其他方法来纠正此问题,或者我是否应该用正确的文件分隔符替换字符串?

使用原始字符串文字

filename = r'D:\Sample\Project\testXcl\data.xlsx'

指定路径名时,请转义反斜杠或使用:


+1用于研究精神:您实际找到了可转义符号的列表:)
p = 'D:\\Sample\\Project\\testXcl\\data.xlsx'
p = r'D:\Sample\Project\testXcl\data.xlsx'