Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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 使用'获取文件路径/';_Python_Os.path - Fatal编程技术网

Python 使用'获取文件路径/';

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

如何获取“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','file'+os.sep)

我很好奇:为什么要在文件名上加最后一个斜杠?我正在连接两个os.path.join如果连接两个路径连接,那么代码应该像
os.path.join(os.path.join('path','to','file')、os.path.join('second','path'))
一切都会好起来的。