python - Get relative path of file1 (relative to path of file2, file1 is in subfolder of file2) -
I have file_css in some folds. I want to include it in the path of file_inc (via CSS included). File_inc file_css can be in subfolder (direct subfolder or 2-3 levels); Also be in the same folder as file_css
How to get the relative path of file_inc?
Example
- file_css = "d: \ my \ my.css" file_inc = "d: \ my \ in \ more \ inc.css" string me "more \ Cc \ "get it
- OK to fix it: file_css =" d: \ my \ my.css "file_inc =" d: \ inc.css "I
Os .path.relpath : & gt; & Gt; & Gt; Os.path.relpath ('/ foo / bar / baz', '/ foo') 'bar / hawk' & gt; & Gt; & Gt; Os.path.sep = '\\' # I need this because I do not have DOS / CPM / VX / NT & gt; & Gt; & Gt; Os.path.relpath ('c: \\ foo \\ bar \\ baz', 'c: \\ foo') 'bar / hawk'with
Merge Dirname:def css_relative_path (html_path, css_path): return os.path.relpath (css_path, os.path.dirname (html_path)) & gt; & Gt; & Gt; Css_relative_path ('/ foo / bar / baz.html', '/foo/bar/css/baz.css') 'css / baz.css'
Comments
Post a Comment