Author Topic: Rate And correct my script  (Read 564 times)

0 Members and 1 Guest are viewing this topic.

Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
Rate And correct my script
« on: May 27, 2013, 07:43:15 am »
this is a "short " script that actually works  ;) .
Its intended to walk through a directory and search for a specified file (specified by extension) and check how long it has been laying around and delete it if it doesn't meet the criteria given for surviving files.
feel free to check it out and let me know how i could have done it better.
regards,
pyte.


Code: [Select]
import pyttsx
import datetime
import os
import fnmatch
import time


#clearing my throat
engine = pyttsx.init()
rate =engine.getProperty("rate")
engine.setProperty('rate', rate-70)


#definations and staff ;)
rootPath = '/some/path'
engine.say("enter extention type and press enter eg mp3 or txt")
engine.runAndWait()
Extension = raw_input(">>  ")
pattern = ("*."+ Extension)
last_modified = datetime.datetime.fromtimestamp(os.path.getmtime(rootPath))


#walking down the directories
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        if datetime.datetime.now() - last_modified > datetime.timedelta(hours =1):
            engine.say(filename)
            engine.say( " was last_modified on ")
            engine.say(last_modified)
            engine.say("deleting files")
            engine.runAndWait()
            print filename
clean = os.path.join(root, filename)
os.remove(clean)


#only deletes the last file instead of all files found. how do i solve this?
If you don't go into the tiger's cave, how will you get the cub?

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Rate And correct my script
« Reply #1 on: May 27, 2013, 08:13:18 am »
I suggest you make a list of the files and for loop it.
That way os.remove() applies to all the files.

Do something like;
Code: [Select]
toBeRemoved = []
..
..
..
..

toBeRemoved.append(filename)
..
..
..
..


for entry in toBeRemoved:
    os.remove(entry)


You get the point.


Also I suggest you keep the speech part seperate from the file operations to clean things up.
Something like:
Code: [Select]
def talkback(what):
   
    if what == blabla:
        blablabla
etc


« Last Edit: May 27, 2013, 08:16:09 am by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
Re: Rate And correct my script
« Reply #2 on: May 27, 2013, 08:36:34 am »
proxx,
Thanx.
and for sure EZRocks!
If you don't go into the tiger's cave, how will you get the cub?