Python 使用'获取文件路径/';
如何获取“path/to/file/” 这缺少最后一个“/”Python 使用'获取文件路径/';,python,os.path,Python,Os.path,如何获取“path/to/file/” 这缺少最后一个“/” In [2]: os.path.join('path', 'to', 'file') Out[2]: 'path/to/file' 这无助于: In [3]: os.path.join('path', 'to', 'file', '/') Out[3]: '/' 只需自己添加它:os.path.join('path','to','file')+os.path.sep尝试一下:os.path.join('path','to','fi
In [2]: os.path.join('path', 'to', 'file')
Out[2]: 'path/to/file'
这无助于:
In [3]: os.path.join('path', 'to', 'file', '/')
Out[3]: '/'
只需自己添加它:
os.path.join('path','to','file')+os.path.sep
尝试一下:os.path.join('path','to','file'+os.sep)我很好奇:为什么要在文件名上加最后一个斜杠?我正在连接两个os.path.join如果连接两个路径连接,那么代码应该像os.path.join(os.path.join('path','to','file')、os.path.join('second','path'))
一切都会好起来的。