~Python ?

Jeff Epler jepler at inetnebr.com
Tue Dec 26 19:49:51 EST 2000

On Tue, 26 Dec 2000 06:03:40 GMT, Gerson Kurz
 <gerson.kurz at t-online.de> wrote:
>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.
>(I think Perl, Pythons ugly twin-brother, does)

I'd have to say that string-substitution-based languages screw
me up about ~2/3 of the time.  It must be something about the way my mind
works.      ^^^^
            hee hee, get it?

Seriously, the os.path module includes goodies such as os.path.expanduser
and os.path.expandvars.

If you want shorthand for this when you do an 'open', try the following:
	import __builtin__, os
	def open(file, *args):
		file = os.path.expanduser(file)
		file = os.path.expandvars(file)
		return __builtin__.open(file, *args)

>>> open("~/.bashrc").readlines()
['# .bashrc\012', '\012', '# User specific aliases and functions\012', '\012', '# Source global definitions\012', 'if [ -f /etc/bashrc ]; then\012', '\011. /etc/bashrc\012', 'fi\012']


More information about the Python-list mailing list