在Python中查找和比较文件

时间:2015-11-27 01:14:48

标签: python sorting find typeerror

我试图在变量name_c中找到一个名为“url.html”的文件。 如果我的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

这是什么意思?

1 个答案:

答案 0 :(得分:1)

您已在某处导入了time模块(尽管您的代码没有显示):

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

我怀疑你打算改用词典time_d