如何修复 Python "TypeError: utime() takes no keyword arguments"
问题:
你正在尝试使用 os.utime() 设置文件访问/修改日期,如下所示:
python_utime_keyword_args_fix.py
os.utime(filename, times=(myatime, mymtime))但你看到类似这样的错误消息:
utime_typeerror.txt
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解决方案
使用 os.utime(..., times=(...)) 是Python 3 语法,所以如果可以的话使用 Python 3!
如果你仍然需要使用 Python 2.x,只需从源代码中移除 times=:
utime_fix.py
os.utime(filename, (myatime, mymtime))此代码在 Python 2 和 Python 3 中都能正常工作。
注意在撰写此文章时,Python 2 的退役仅剩几个月!
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow