Calling all Python experts

Michael Hipp Michael
Wed Dec 20 07:07:47 PST 2006


David Bandel wrote:
> Folks,
> 
> OK, well, someone took a perfectly good idea originally coded in Perl
> and decided he liked Python better :-(.
> 
> Well, I need to try to figure out how Python handles grabbing
> arguments to programs.  Specifically, sendmail is calling procmail,
> which runs a filter in Python (if only this were Perl I could fix it,
> but I don't feel like learning enough Python to translate the entire
> program to Perl).
> 
> The install suggests something like:
> "/var/mail/%s" % os.environ.get("USER")
> 
> Geez, what atrocious syntax.  Might as well be Klingon.
> 
> unfortunately, the above returns a filter error: no such file /var/mail/None.
> duh. That None is supposed to be the user's name that sendmail passed
> to procmail.
> 
> Anyone have any ideas how to code this correctly?  Note that, since
> most users have /bin/false as their shell, I had to declare:
> SHELL=/bin/sh
> in the user's .procmailrc

The guy probably comes from a C++ background and just can't accept that it's 
often possible to write things in a readable fashion. :-)

Anyway, that statement works when run from the Python interpreter on my Ubuntu 
box. I'd have written it this way:

   "/var/mail/" + os.environ.get("USER")

So if it's not working then USER must not be defined in the environment. Or 
else the Python installation is borked.

Here's the full output of what I did:

$ python
Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import os
 >>> os.environ.get("USER")
'michael'
 >>> "/var/mail/" + os.environ.get("USER")
'/var/mail/michael'
 >>> os.environ.get("MAIL")
'/var/mail/michael'

Hope this helps,
Michael




More information about the Linux-users mailing list