fixes #4277 this time in pure python (no calls to bash)

This (and elsewhere) is valid but not recommended python. Please put "if" and "then" on separate lines so if something changes later, the diffs make sense.