Replace shell script by a more robust python script