[英]Get relative path of file1 (relative to path of file2, file1 is in subfolder of file2)
我在某些文件夾中有file_css。 我想包括(通過CSS包括)file_inc的路徑。 file_inc可能位於file_css的子文件夾中(直接子文件夾或2-3級); 甚至與file_css位於同一文件夾中。
如何獲得file_inc的相對路徑?
例。
使用os.path.relpath
:
>>> os.path.relpath('/foo/bar/baz', '/foo')
'bar/baz'
>>> os.path.sep = '\\' # I need this because i'm not on dos/cpm/vax/nt
>>> os.path.relpath('c:\\foo\\bar\\baz', 'c:\\foo')
'bar/baz'
結合dirname
:
def css_relative_path(html_path, css_path):
return os.path.relpath(css_path, os.path.dirname(html_path))
>>> css_relative_path('/foo/bar/baz.html', '/foo/bar/css/baz.css')
'css/baz.css'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.