2008/07/08

Set environment variables in python

client.py

#!/usr/bin/env python

import os,datetime

starttimestr=datetime.datetime.now().strftime('%Y%m%d%M%S')

os.environ['starttime']=starttimestr

os.system('t1.sh')

t1.sh

#!/bin/bash

echo $starttime

runt1.py

#!/usr/bin/env python

import os,datetime

starttime=datetime.datetime.strptime(os.environ['starttime'],'%Y%m%d%M%S')

print starttime

environment variables can be transferred in this way. Such changes to the environment affect subprocesses started with os.system(), popen() or fork() and execv(). Availability: most flavors of Unix, Windows.

No comments: