Convert bash script to python