porting bash script to python