newbie: Windows os.getenv(...

Matt Gerrans matt.gerrans at hp.com
Mon Oct 20 21:39:10 EDT 2003


"Martin" wrote:
> ...but not
> d1 = os.getenv("%date%")

DATE isn't really an environment variable (like TIME, CD, etc.), it is just
a little convenience that the command interpreter gives you for making batch
file ("shell script") tasks (such as logging) a little less difficult.   You
could get the fake date "environment variable" from the command interpreter
like this:

   reader = os.popen( 'cmd /c echo %date%', 'r' )
   datestring =  reader.read().strip()

But I think it would probably make a lot more sense to just use Python's
time module, which has a lot more to offer.

> Also I cannot change environment variables with
> os.putenv("any" , "something")

As for setting environment variables, there are (at least) two things to
consider:

1) Do you want it to persist for the "current session" after your script
runs?   This is a toughie.  I had a little module to do this back in the DOS
days, that involved messing with the PSP, but that doesn't fly in NT/XP/2K!
:-(   The closest you can come is to have your script write a batch that is
subsequently called by the caller.

2) If you want to modify the current user's or the system's environment
permanently, but don't care so much about the "current session" then you
tweak the registry then broadcast a system message (to tell other apps (most
of whom are not paying attention!) that you made the change).   If you need
more info on this and the searching suggested by Peter Hansen elsewhere in
this thread doesn't turn it up, post back here and I'll dump the full (gory)
details.

> Do you have a clue?

Hey, you don't have to rub it in!






More information about the Python-list mailing list