How to fix Python “TypeError: utime() takes no keyword arguments”

Problem:

You are trying to set file access/modification dates using os.utime() like this:

os.utime(filename, times=(myatime, mymtime))

but you see an error message like this:

Traceback (most recent call last):
  File "utime.py", line 6, in <module>
    os.utime("myfile.txt", times=(myatime, mymtime))
TypeError: utime() takes no keyword arguments

Solution:

Using os.utime(..., times=(...)) is Python 3 syntax, so use Python 3 if you can!

In case you still need to use Python 2.x, just remove times= from the source code:

os.utime(filename, (myatime, mymtime))

This code will work fine with both Python 2 and Python 3.

Note that Python 2 retirement is just a few months away at the time of writing this post!