Python find difference between file paths

2024/10/8 2:24:59

I have a bunch of file paths, such as:

path1 = "./base/folder1/subfolder"
path2 = "./base/folder2/"

I am trying to write a function that can give me the relative difference between the paths. Using the paths above:

>>> get_path_difference(path1, path2)
"../../folder2"
>>> get_path_difference(path2, path1)
"../folder1/subfolder"

I've had a look through the os.path module, since it seems like this should be a common thing, but either I don't know the terminology or it isn't there.

Answer

You can use os.path.relpath:

>>> path1 = "./base/folder1/subfolder"
>>> path2 = "./base/folder2/"
>>> import os
>>> os.path.relpath(path1, path2)
'../folder1/subfolder'
>>> os.path.relpath(path2, path1)
'../../folder2'
https://en.xdnf.cn/q/70173.html

Related Q&A

Update range of colorbar in matplotlib

I want to update a contourf plot within a function, which works fine. However, the range of the data changes and I therefore also have to update the colorbar. That is where I fail to do so.Please see f…

Anaconda - arrow keys not work well in python shell

I installed Anaconda3 on manjaro (with i3wm and Urxvt). When I go into python interpreter, it is OK to type python script and execute. But when key arrows are pressed to call up history, everything mes…

couldnt remove origin point in matplotlib polycollection

I have tried an example with PolyCollection from matplotlib tutorials and noticed one strange thing. I couldnt remove this points from axes origin see fig. How do I manage this?from mpl_toolkits.mplot…

How to read .odt using Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.The com…

Modifying Django settings in tests

From Django docs:You shouldn’t alter settings in your applications at runtime. Forexample, don’t do this in a view:from django.conf import settingssettings.DEBUG = True # Dont do this!The only plac…

How To Use Django Cycle Tag

This hopefully is a pretty easy question. My endgoal is to be able to view my database entries in a table which I can sort via the column headers. I have read the documentation on cycle tags, but dont …

Multiple linear regression with python

I would like to calculate multiple linear regression with python. I found this code for simple linear regressionimport numpy as npfrom matplotlib.pyplot import *x = np.array([1, 2, 3, 4, 5])y = np.arra…

How to find source of error in Python Pickle on massive object

Ive taken over somebodys code for a fairly large project. Im trying to save program state, and theres one massive object which stores pretty much all the other objects. Im trying to pickle this object,…

What is the most idiomatic way to index an object with a boolean array in pandas?

I am particularly talking about Pandas version 0.11 as I am busy replacing my uses of .ix with either .loc or .iloc. I like the fact that differentiating between .loc and .iloc communicates whether I a…

Error: Command failed with rc=65536 python and mod_wsgi

im having this problem: im runing pythonbrew to get python2.7, and so i re-compiled mod_wsgi to use the 2.7 python. to that end, i followed this tutorial: code.google.com/p/modwsgi/wiki/QuickInstallati…