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.

Recommended Answers

All 3 Replies

Your error was very clear.

the

os.stat("path to file")

But you have only provided the file name.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.