You could use 2to3
and only apply the fix for print statement -> print function.
2to3 --fix=print [yourfiles] # just displays the diff on stdout
2to3 --fix=print [yourfiles] --write # also saves the changes to disk
This should automatically handle all those strange cases, and then converting print functions to logging functions should be a straightforward find-and-replace with, e.g., sed
.
If you don't have the shortcut for the 2to3
script for some reason, run lib2to3
as a module instead:
python -m lib2to3 --fix=print .