Using the groupby method in Python, example included
Posted
by randombits
on Stack Overflow
See other posts from Stack Overflow
or by randombits
Published on 2010-04-02T01:36:29Z
Indexed on
2010/04/02
1:43 UTC
Read the original article
Hit count: 424
python
Trying to work with groupby so that I can group together files that were created on the same day. When I say same day in this case, I mean the dd part in mm/dd/yyyy. So if a file was created on March 1 and April 1, they should be grouped together because the "1" matches. Here's the code I have so far:
#!/usr/bin/python
import os
import datetime
from itertools import groupby
def created_ymd(fn):
ts = os.stat(fn).st_ctime
dt = datetime.date.fromtimestamp(ts)
return dt.year, dt.month, dt.day
def get_files():
files = []
for f in os.listdir(os.getcwd()):
if not os.path.isfile(f): continue
y,m,d = created_ymd(f)
files.append((f, d))
return files
files = get_files()
for key, group in groupby(files, lambda x: x[1]):
for file in group:
print "file: %s, date: %s" % (file[0], key)
print " "
The problem is, I get lots of files that get grouped together based on the day. But then I'll see multiple groups with the same day. Meaning I might have 4 files grouped that were created on the 17th. Later on I'll see another unique set of 2 files that are also created on the 17th. Where am I going wrong?
© Stack Overflow or respective owner