Converted shell script to pythong (untested)