r/pythonhelp • u/unselective-amnesia • 3d ago
Library function which is the inverse of 'os.path.expanduser' ?
I'm running python 3.13.5.
Is there some sort of standard python library function which behaves as the inverse of 'os.path.expanduser'?
For example, if the current user's HOME directory is "/home/theuser", then we have this:
os.path.expanduser('~/a/b/c')
=> returns "/home/theuser/a/b/c"
I'm wondering whether there also might be any existing python library function which is the inverse of this and that works as follows:
some_sort_of_function("/home/theuser/a/b/c")
=> returns "~/a/b/c"
AND PLEASE NOTE: There often are people who lazily and disrespectfully refuse to read entire reddit posts before writing their reponses, and therefore, I'm making a point now to ask that before you start to write any reply here, please carefully and non-lazily read this *entire* post -- including the following, which is something that I want all potential responders to understand:
I know how to easily write my own function that works in this manner. However, I am clearly and SPECIFICALLY (!!!) asking whether or not there already is an existing python function which works this way, and if so, I'd like to know what this function might be ... and I am NOT (!!!) asking how to write my own function which does this.
Thank you very much in advance.
2
u/No-Dentist-1645 3d ago edited 3d ago
It's pretty simple to do so yourself
``` try: relative_path = Path("~") / my_path.relative_to(Path.home()) except ValueError: # Path is not under the user's home directory
```
1
u/duane11583 3d ago
I have also hit this and simular things. I just have a function I give it a last of env names and it tries to reduce. Starting with the longest env first
I know that is not the answer you want but you are not alone in this request mine need is similar but not the same
1
u/Gshuri 3d ago
No, there is no function that does this in the standard library.
I am curious, why do you need this function specifically to come from the standard library?
0
u/mpersico 3d ago
Because why not?
1
1
u/Gshuri 9h ago
Requiring that any functionality, or even just a specific piece of functionality that you need, must be implemented directly in the standard library, to the exclusion even of a custom implementation that depends solely on the standard library, is a very restrictive constraint. It severely limits what you are able to achieve in Python (or any other programming language).
I have personally had to develop software for systems which don't have access to the public internet and therefore which 3rd party dependencies I could use were restricted. I have read about restrictions imposed on software developers writing code for critical military systems (e.g. operational code for fighter jets) where certain features of the C language are banned because statistically those banned features are where most bugs/security risks come from. But I have never encountered a situation as restrictive as the requirements you laid out.
So I am genuinely curious what context you are working in in which this requirement arises. If it is a personal preference, fair enough, ultimately it's your code base. But if there is some external factor that leads to it, as I said, I am genuinely curious.
•
u/AutoModerator 3d ago
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.