hey guys and girls i am having a bit of a problem with my code

import os,string,sys,pprint,time,glob
from datetime import datetime, timedelta
fileList = os.walk('.')#if i use glob.glob it does not show any files
now = datetime.now()
TWA = now + timedelta(days=-1)
#print(TWA)
files=str()
for files in fileList:
    fileStats = os.stat(files)#BUT using os.walk causes this function to fail
    daysOld = (int(now - fileStats.st_mtime))
    if daysOld < TWA:
        os.remove(files)
print('Deleting: '+ files)

i can use glob.glob and run the code with no errors but it will not print the files
so i tried using os.walk as its quicker and more effective.

the only thing is that now my code to find the files date has stopped working and receive this error

Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\My Documents\test dir\dir1\deletetv.py", line 11, in <module>
    fileStats = os.stat(files)
TypeError: Can't convert 'tuple' object to str implicitly

if any one could help me or point me in the right direction to why it would fail.

i have a basic knowledge of python and love learning new features but i cant seem to find anything which would stop os.stat working!

thanks for looking people.

Edited 5 Years Ago by danholding: my poor spelling :(

fileStats = os.stat(files)
TypeError: Can't convert 'tuple' object to str implicitly

This says that "files" is tuple. Print "type(files)" and files itself if you want to on the line before the error.

os.walk() returns a triple: (path, dirnames, filenames) where the second and third items are themselves (possibly empty) lists.

Beware that removing files while os.walk -ing in the vicinity "may cause unstable behavior" :). The safest way is to collect all the things that will need removing, then do them all at once when the walk is over.

This article has been dead for over six months. Start a new discussion instead.