Author Topic: Searching for files with multiple extensions.  (Read 1179 times)

0 Members and 1 Guest are viewing this topic.

Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
Searching for files with multiple extensions.
« 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! "
« Last Edit: June 27, 2013, 07:33:13 pm by RedBullAddicted »
If you don't go into the tiger's cave, how will you get the cub?

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Searching for files with multiple extensions.
« Reply #1 on: May 24, 2013, 07:53:41 am »
http://stackoverflow.com/questions/3964681


You could loop over the answer in the above link for each extension.

Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
Re: Searching for files with multiple extensions.
« Reply #2 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.
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: Searching for files with multiple extensions.
« Reply #3 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.
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: Searching for files with multiple extensions.
« Reply #4 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.
« Last Edit: June 27, 2013, 07:33:24 pm by RedBullAddicted »
If you don't go into the tiger's cave, how will you get the cub?