proposal: os.path.joinuser()
Hello I realize I am using a lot this pattern:
os.path.join(os.path.expanduser('~'), 'something', 'here') '/Users/tarek/something/here'
It's quite complicated, and not really intuitive. What about adding in os.path a "joinuser()" equivalent function, e.g.
os.path.joinuser('something', here') '/Users/tarek/something/here'
With an optional "user" argument when you want to specify the ~user
os.path.joinuser('something', here', user="foo") '/Users/bill/something/here'
That would be, in my opinion, much more explicit & readable. If there's already something like that in the stdlib, forgive my ignorance! Cheers Tarek
On Tue, Sep 23, 2014 at 9:19 AM, Tarek Ziadé <tarek@ziade.org> wrote:
Hello
I realize I am using a lot this pattern:
os.path.join(os.path.expanduser('~'), 'something', 'here') '/Users/tarek/something/here'
It's quite complicated, and not really intuitive.
I don't find that complicated, just a bit verbose perhaps, but it's definitively clear what it's doing and it is also explicit. os.path.joinuser('something', here') would probably be a bit less verbose but IMO it's not a good enough reason to introduce a new API. -- Giampaolo - http://grodola.blogspot.com
Le 23/09/14 09:52, Giampaolo Rodola' a écrit :
On Tue, Sep 23, 2014 at 9:19 AM, Tarek Ziadé <tarek@ziade.org <mailto:tarek@ziade.org>> wrote:
Hello
I realize I am using a lot this pattern:
>>> os.path.join(os.path.expanduser('~'), 'something', 'here') '/Users/tarek/something/here'
It's quite complicated, and not really intuitive.
I don't find that complicated, just a bit verbose perhaps, but it's definitively clear what it's doing and it is also explicit.
~ is a Unix notion I think, and since expanduser() works under Windows, I don't think it's that intuitive and explicit. Unless we'd change it so we omit "~" => e.g. os.path.expanduser() and os.path.expanduser('specificuser') Cheers Tarek
Le 23/09/14 14:32, Tarek Ziadé a écrit : [...]
~ is a Unix notion I think, and since expanduser() works under Windows, I don't think it's that intuitive and explicit.
Unless we'd change it so we omit "~" =>
e.g. os.path.expanduser() and os.path.expanduser('specificuser') I would even say that the api name "expanduser" is redundant with the fact that you absolutely need to pass a '~' as a first char if you want it to do something at all.
The more I think about it, the more I find it unintuitive
On Tue, Sep 23, 2014 at 2:37 PM, Tarek Ziadé <tarek@ziade.org> wrote:
Le 23/09/14 14:32, Tarek Ziadé a écrit : [...]
~ is a Unix notion I think, and since expanduser() works under Windows, I don't think it's that intuitive and explicit.
Unless we'd change it so we omit "~" =>
e.g. os.path.expanduser() and os.path.expanduser('specificuser') I would even say that the api name "expanduser" is redundant with the fact that you absolutely need to pass a '~' as a first char if you want it to do something at all.
The more I think about it, the more I find it unintuitive
What about something like os.path.homedir or os.homedir? You could use, for example, homedir() to get the home directory of the current user, and homedir('username') to get the home directory of a given user (if that is possible).
Le 23/09/14 14:51, Todd a écrit :
What about something like os.path.homedir or os.homedir? You could use, for example, homedir() to get the home directory of the current user, and homedir('username') to get the home directory of a given user (if that is possible).
Sounds way better!
On 23 September 2014 22:52, Tarek Ziadé <tarek@ziade.org> wrote:
Le 23/09/14 14:51, Todd a écrit :
What about something like os.path.homedir or os.homedir? You could use, for example, homedir() to get the home directory of the current user, and homedir('username') to get the home directory of a given user (if that is possible).
Sounds way better!
Given the naming of site.getuserbase(), site.getusersite(), and os.expanduser(), a simple "os.userdir()" (to parallel "os.curdir()") may be appropriate. Then making a path that is relative to the user dir absolute would be: abspath = os.path.join(os.userdir(), relpath) While the inverse operation would be: relpath = os.path.relpath(abspath, start=os.userdir()) os.path.expanduser() would then be primarily about dealing with *longer* paths that have "~" embedded, rather than the degenerate case of "os.expanduser('~')". Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
Le 23/09/14 15:03, Nick Coghlan a écrit :
On 23 September 2014 22:52, Tarek Ziadé <tarek@ziade.org> wrote:
Le 23/09/14 14:51, Todd a écrit :
What about something like os.path.homedir or os.homedir? You could use, for example, homedir() to get the home directory of the current user, and homedir('username') to get the home directory of a given user (if that is possible). Sounds way better! Given the naming of site.getuserbase(), site.getusersite(), and os.expanduser(), a simple "os.userdir()" (to parallel "os.curdir()") may be appropriate.
Then making a path that is relative to the user dir absolute would be:
abspath = os.path.join(os.userdir(), relpath)
While the inverse operation would be:
relpath = os.path.relpath(abspath, start=os.userdir())
os.path.expanduser() would then be primarily about dealing with *longer* paths that have "~" embedded, rather than the degenerate case of "os.expanduser('~')".
Regards, Nick.
This is getting better and better :-)
On Tue, Sep 23, 2014 at 5:19 PM, Tarek Ziadé <tarek@ziade.org> wrote:
I realize I am using a lot this pattern:
os.path.join(os.path.expanduser('~'), 'something', 'here') '/Users/tarek/something/here'
It's quite complicated, and not really intuitive.
Not every one-liner needs to be in the stdlib. def joinuser(*parts, user=''): return os.path.join(os.path.expanduser('~'+user), *parts) Suggestion: Build up your own utilities module (I used to call it "oddsends" - Odds & Ends - although back when I started, it was a Q-Basic file from which I would copy and paste code, rather than a Python module from which I'd import), and put this kind of convenience function in it. Then it's as simple as: from utils import joinuser at the top of your script. ChrisA
On Tue, Sep 23, 2014 at 5:19 PM, Tarek Ziadé <tarek@ziade.org> wrote:
I realize I am using a lot this pattern:
os.path.join(os.path.expanduser('~'), 'something', 'here') '/Users/tarek/something/here'
It's quite complicated, and not really intuitive. Not every one-liner needs to be in the stdlib. Unless it makes a lot of sense - thus my e-mail here to see if it gets
Le 23/09/14 10:40, Chris Angelico a écrit : traction :-)
On Tue Sep 23 2014 at 3:20:09 AM Tarek Ziadé <tarek@ziade.org> wrote:
Hello
I realize I am using a lot this pattern:
os.path.join(os.path.expanduser('~'), 'something', 'here') '/Users/tarek/something/here'
It's quite complicated, and not really intuitive.
What about adding in os.path a "joinuser()" equivalent function, e.g.
os.path.joinuser('something', here') '/Users/tarek/something/here'
With an optional "user" argument when you want to specify the ~user
os.path.joinuser('something', here', user="foo") '/Users/bill/something/here'
That would be, in my opinion, much more explicit & readable.
If there's already something like that in the stdlib, forgive my ignorance!
You probably want to propose a change off of pathlib instead of os.path since that's the future of high-level path manipulation in the stdlib.
On Tue, 23 Sep 2014 15:18:40 +0000 Brett Cannon <brett@python.org> wrote:
You probably want to propose a change off of pathlib instead of os.path since that's the future of high-level path manipulation in the stdlib.
Speaking of which: http://bugs.python.org/issue19776 http://bugs.python.org/issue19777 Regards Antoine.
Le 23/09/14 17:50, Antoine Pitrou a écrit :
On Tue, 23 Sep 2014 15:18:40 +0000 Brett Cannon <brett@python.org> wrote:
You probably want to propose a change off of pathlib instead of os.path since that's the future of high-level path manipulation in the stdlib. Speaking of which: http://bugs.python.org/issue19776 http://bugs.python.org/issue19777 oh ok, so I guess http://bugs.python.org/issue19777 is it. - beside the name, and location
Is there anything I can do to help moving this forward ?
Regards
Antoine.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Wed, 24 Sep 2014 10:08:45 +0200 Tarek Ziadé <tarek@ziade.org> wrote:
Le 23/09/14 17:50, Antoine Pitrou a écrit :
On Tue, 23 Sep 2014 15:18:40 +0000 Brett Cannon <brett@python.org> wrote:
You probably want to propose a change off of pathlib instead of os.path since that's the future of high-level path manipulation in the stdlib. Speaking of which: http://bugs.python.org/issue19776 http://bugs.python.org/issue19777 oh ok, so I guess http://bugs.python.org/issue19777 is it. - beside the name, and location
Is there anything I can do to help moving this forward ?
You can write a patch! cheers Antoine.
participants (7)
-
Antoine Pitrou
-
Brett Cannon
-
Chris Angelico
-
Giampaolo Rodola'
-
Nick Coghlan
-
Tarek Ziadé
-
Todd