Python 3 fixes for chmod and exceptions

os.chmod call is fixed to use proper flag values with bitwise or masking for file mode. Info on the flags here, being no difference between python 2 and 3:
http://docs.python.org/2/library/stat.html
http://docs.python.org/3/library/stat.html

Raising exceptions is now more strict, using parenthesis instead of commas. And exception handling uses 'as' to avoid comma confusion.

Actually, the 'as' might be removed entirely, as the variable 'k' is never actually used in that statement...