os.popen('alias')

Lars Kellogg-Stedman 8273grkci8q8kgt at jetable.net
Fri Nov 18 16:54:37 EST 2005


>                 import os
>                 for line in os.popen('alias').readlines():
>                           print line
>
>
> No aliases are printed.
>
> I started python from an bash environment that had many aliases
> defined. I expected to see the list of aliases from within the
> interactive python shell. What could I do to see those aliases defined
> in the shell from where I started python?

You can't, really.  The 'alias' command is a shell built-in, not an external
command, so you can't meaningfully run it from a Python script (and any
aliases defined in your shell will probably not be available to Python).

Matters are complicated a little bit because when you use os.popen(),
your command line is actually passed to *a* shell, usually /bin/sh, so
the final command line looks like this:

  /bin/sh -c 'alias'

However, even if /bin/sh is actually bash, dotfiles such as .profile and
.bashrc aren't read when using the '-c' option.

If you really want to do something to your bash aliases with python, you
could pipe them into a python command:

  $ alias | python myscript.py

-- Lars

-- 
Lars Kellogg-Stedman <8273grkci8q8kgt at jetable.net>
This email address will expire on 2005-11-23.




More information about the Python-list mailing list