Google Groups Home
Help | Sign in
Find Files in a Folder Between 2 Dates
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Gregory Plantaine  
View profile
 More options 4 Dec 2008, 22:38
Newsgroups: comp.lang.python
From: Gregory Plantaine <gamersu...@gmail.com>
Date: Thu, 4 Dec 2008 14:38:37 -0800 (PST)
Local: Thurs 4 Dec 2008 22:38
Subject: Find Files in a Folder Between 2 Dates
Is there a way to find all the files in a folder, between 2 dates?

For example:

Firstdate = 200801010000
Seconddate = 200801020000

Find all the files in C:\Folder that are between Firstdate and
SecondDate.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Chase  
View profile
 More options 4 Dec 2008, 23:42
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Thu, 04 Dec 2008 17:42:19 -0600
Local: Thurs 4 Dec 2008 23:42
Subject: Re: Find Files in a Folder Between 2 Dates

> Is there a way to find all the files in a folder, between 2 dates?

> For example:

> Firstdate = 200801010000
> Seconddate = 200801020000

> Find all the files in C:\Folder that are between Firstdate and
> SecondDate.

This should do it:

   import time
   import os
   firstdate = "200801010000"
   seconddate = "200801020000"
   def get_timestamp(s):
     return time.mktime(time.strptime(s, "%Y%m%d%H%M"))
   start = get_timestamp(firstdate)
   end = get_timestamp(seconddate)
   location = '/path/to/wherever/'
   files = [fname
     for fname in os.listdir(location)
     if start <=
       os.stat(os.path.join(location, fname)).st_mtime
       <= end
     ]
   print files

The magic is the the os.stat(f).st_mtime to determine when the
associated timestamp, the os.listdir() to get the available files
in a directory, and the list-comprehension to filter to only the
ones you want.

-tkc


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gregory Plantaine  
View profile
 More options 5 Dec 2008, 22:41
Newsgroups: comp.lang.python
From: Gregory Plantaine <gamersu...@gmail.com>
Date: Fri, 5 Dec 2008 14:41:08 -0800 (PST)
Local: Fri 5 Dec 2008 22:41
Subject: Re: Find Files in a Folder Between 2 Dates
That worked perfectly!

Thanks Tim!

Since we can print the files, does that mean the list of files is in a
tuple, or something?  Would there be a way to further split up the
file names?

For example, now that the files are processed into the list, we want
to look through that list to find different filetypes.

files

C:\Folder\File_200812051439.111
C:\Folder\File_200812051539.222

Can we split up .111 files?

Actually, where would I look something like this up?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile
 More options 5 Dec 2008, 23:14
Newsgroups: comp.lang.python
From: John Machin <sjmac...@lexicon.net>
Date: Fri, 5 Dec 2008 15:14:39 -0800 (PST)
Local: Fri 5 Dec 2008 23:14
Subject: Re: Find Files in a Folder Between 2 Dates
On Dec 6, 9:41 am, Gregory Plantaine <gamersu...@gmail.com> wrote:

> That worked perfectly!

> Thanks Tim!

> Since we can print the files, does that mean the list of files is in a
> tuple, or something?  Would there be a way to further split up the
> file names?

> For example, now that the files are processed into the list, we want
> to look through that list to find different filetypes.

> files

> C:\Folder\File_200812051439.111
> C:\Folder\File_200812051539.222

*DANGER* It looks like you are interested in the timestamps that are
embedded in the names of the files. Tim's code assumes [reasonably
given that your problem description was ambiguous and had no examples
of good and bad results] that you are interested in the last
modification time of the file. You may say "same thing". Yes, same
thing, until somebody sucks a file into a text editor, messes with it,
and saves it again. No, Murphy's Law has not been repealed.

> Can we split up .111 files?

> Actually, where would I look something like this up?

In the Library Reference Manual ... there are all sorts of goodies in
the os and os.path modules e.g. like those used by Tim; make sure you
read the docs on the methods Tim used so that you understand what's
happening.

HTH,
John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gregory Plantaine  
View profile
 More options 11 Dec 2008, 19:02
Newsgroups: comp.lang.python
From: Gregory Plantaine <gamersu...@gmail.com>
Date: Thu, 11 Dec 2008 11:02:25 -0800 (PST)
Local: Thurs 11 Dec 2008 19:02
Subject: Re: Find Files in a Folder Between 2 Dates
On Dec 5, 3:14 pm, John Machin <sjmac...@lexicon.net> wrote:

Thanks for the advice John!

I was going though the Manual, but I'm having some trouble figuring
out how to iterate through each line.

So from the same example, we've already created a list called "lists".
Now how do I iterate through each line?

For eachline in lists
    Find all .111 files.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gregory Plantaine  
View profile
 More options 12 Dec 2008, 00:35
Newsgroups: comp.lang.python
From: Gregory Plantaine <gamersu...@gmail.com>
Date: Thu, 11 Dec 2008 16:35:33 -0800 (PST)
Local: Fri 12 Dec 2008 00:35
Subject: Re: Find Files in a Folder Between 2 Dates
On Dec 11, 11:02 am, Gregory Plantaine <gamersu...@gmail.com> wrote:

Ok, I figured it out. Actually more simple than I thought:

for eachfile in files:
        if eachfile.endswith(".111"):
                print eachfile

Another somewhat related question, instead of defining the firstdate,
how would we do this?

firstdate = secondate - 7 days


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google