Leo
Leo

Reputation: 423

Finding and comparing files in Python

I tried to find a file named "url.html" stored in the variable name_c. If there is more than the same file on my mac, I'll want to see the last modified. I wrote this:

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]

The error is

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

What does it mean?

Upvotes: 3

Views: 45

Answers (1)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24153

You have imported the time module somewhere (although your code doesn't show it):

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

I suspect you meant to use the dictionary time_d instead.

Upvotes: 1

Related Questions