~Python ?

David Porter jcm at bigskytel.com
Tue Dec 26 03:09:26 EST 2000


* Gerson Kurz <gerson.kurz at t-online.de>:

> On Linux, ~ usually refers to the users homedir, but not so in Python
> (at least, in ActivePython 2.0). I found out the hard way when using a
> script that issued os.makedirs() relative to ~ - It created an actual
> directory named ~. You can have real fun if you try to deleting an
> actual directory named ~ on Linux, boy. 

It's easy. Just escape it with a backslash: 'rmdir \~'.

> I understand that ~ is probably handled by the shell, but if Python
> doesn't deal with it, its hard to use Python in some kinds of scripts.

os.path.expanduser() will interpret a tilde as the user's home directory:

>>> import os.path
>>> print os.path.expanduser('~')
'/home/dap'

you can also specify the user:

>>> print os.path.expanduser('~root')
'/root'


David




More information about the Python-list mailing list