EvilZone

Programming and Scripting => Scripting Languages => Topic started by: pyte on May 24, 2013, 07:35:30 AM

Title: Searching for files with multiple extensions.
Post by: pyte on May 24, 2013, 07:35:30 AM
Hi guyz,
I have been trying to write a script to search for files with multiple extensions (.txt, .odt, .bat, .Fvl etc )in a directory , i tried using (path/*.*) but it still doesn't work for me.
here is the script i have written .Kindly go through it and point out any errors or mistakes in the script suggesting how to do it better.
thanks in advance.
Regards ,
Pyte


Code: (Python) [Select]
!#usr/bin/python
import os, datetime

Path = "/home/pytbyte/Documents/kim.doc" # this works fine but ("/home/pytbyte/Documents/*.*") doesn't
last_modified = datetime.datetime.fromtimestamp(os.path.getmtime(Path))
print "last_modified:", last_modified
if datetime.datetime.now() - last_modified > datetime.timedelta(hours=50):
    print "too old! "
Title: Re: Searching for files with multiple extensions.
Post by: Fur on May 24, 2013, 07:53:41 AM
http://stackoverflow.com/questions/3964681 (http://stackoverflow.com/questions/3964681)


You could loop over the answer in the above link for each extension.
Title: Re: Searching for files with multiple extensions.
Post by: pyte on May 24, 2013, 08:37:37 AM



was there earlier and that does work for the specified (extension .txt) . in my case i want to handle all extensions at a go since il be working with multiple  in a directory. on the other hand i have noted the os.walk() which would be better for my case.
Thanx.
Title: Re: Searching for files with multiple extensions.
Post by: proxx on May 24, 2013, 09:29:33 AM
I used something similar recently also with os.walk()
Ill post a snipper as soon as I have the code nearby.
Title: Re: Searching for files with multiple extensions.
Post by: pyte on May 24, 2013, 09:52:45 AM
thank. proxx

by the way i did get the right way to search for all extensions:
this is how..


Code: (Python) [Select]
import os
rootPath = '/some/path'
pattern = "*.*"
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        print filename

Staff note: you don't want to double-post again.