為了把原設為 irclog/$tag/$0/%Y/$0.%Y%m%d.log 格式的 irssi log 改成 irclog/$tag/$0/%Y/$0.%Y%m.log (每月一個檔;之前每天放一個檔,感覺資料太難找了),寫了一個小程式把舊的 log 接合起來。
寫的時間稍微有點久,程式看起來也有點累贅;有沒有辦法在二三十行內解決呢?
以下是程式 listing:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
$Id: irclogday2month.py 5 2006-03-21 13:08:39Z yungyuc $
"""
try:
    __revision__ = int( "$Revesion$".split()[1] )
except:
    __revision__ = 0
import sys
from os import makedirs
from os.path import exists, join, basename
from glob import glob
def main():
    sourcedir = "source"
    targetdir = "target"
    networks = [ basename(p) for p in glob(join( sourcedir, "*" )) ]
    for network in networks:
        channels = [ basename(p)
            for p in glob(join( sourcedir, network, "*" )) ]
        for channel in channels:
            years = [ basename(p)
                for p in glob(join( sourcedir, network, channel, "*" )) ]
            for year in years:
                # Actually do text-joining.
                #  Get day strings.
                days = [ basename(p)
                    for p in glob(join( sourcedir,
                        network, channel, year, "*.log" )) ]
                #  Create dictionary keyed by month name (string) containing
                #  day listing
                months = {}
                for day in days:
                    key = day[-8:-6]
                    month = months.get( key, [] )
                    month.append( day )
                    months[key] = month
                #  Write source text file jointly to target text file.
                for key in months:
                    month = months[key]
                    # Sort the day list.
                    month.sort()
                    # Data initially empty.
                    data = ""
                    #  Then joining all files in day list.
                    for day in month:
                        f = open(join(
                            sourcedir, network, channel, year, day ))
                        data += f.read()
                        f.close()
                    # Preparing target containing directory.
                    tdir = join( targetdir, network, channel, year )
                    if not exists( tdir ):
                        makedirs( tdir )
                    # Write to target text file.
                    tfile = "%s.%s%s.log" % (channel, year, key)
                    f = open(join( tdir, tfile ), 'w' )
                    f.write( data )
                    f.close()
                    sys.stdout.write( "%s is written.\n" % join( tdir, tfile ) )
if __name__ == '__main__':
    main()
# vim:set ai et nu sw=4 ts=4 tw=80:
    
        Posted by yungyuc
        at 21:29, 
        
            0 comment, 
        
        
            0 trackback. 
        
        
    
    
    
            Navigate
        
        - Previous: Private saving ryan @2006/03/19
- Next: gVIM 小技巧,別讓視窗那麼小 @2006/03/26
            Add a trackback
        
        Please send trackback to: http://blog.seety.org/everydaywork/2006/3/21/488/trackback/.
            Add a comment
            
        
        
        
    
