在 Python 中查找和比较文件

Finding and comparing files in Python

我试图找到一个名为 "url.html" 的文件存储在变量 name_c 中。 如果我的 mac 上有多个相同的文件,我想查看最后修改的文件。 我写了这个:

import time
import os
from collections import OrderedDict

home = os.path.expanduser('~/')
name_c = 'url.html'

found = []
time_d = {}
for root, dirs, files in os.walk(home):
    dirs[:] = [d for d in dirs if not d == '.Trash']
    if name_c in files:
        found.append(root)

if not found:
    path_w = home
else:
    for times in range(0, len(found)):
        time[times] = found[times], os.stat(found[times]).st_mtime
        time = OrderedDict(sorted(time.items(), key=lambda x: x[1]))
        path_w = time[len(time) - 1][1]

错误是

time[times] = found[times], os.stat(found[times]).st_mtime
TypeError: 'module' object does not support item assignment

这是什么意思?

您已经在某处导入了 time 模块(虽然您的代码没有显示它):

>>> import time
>>> time[4] = 6
Traceback (most recent call last):
TypeError: 'module' object does not support item assignment

我怀疑您打算改用字典 time_d