change open files to python best practice recommendations
handle OS error 17 in a cleaner way