Text 28 May Python - Directory walking

A friend of mine asked for a simple program. I wrote it for him. Guess what this code does ?

import os

dir='/tmp/a/'
other_dir='/tmp/b/'
extension = 'java'
def get_full_name(alternate_top, find_file_name):
    for root, dirs, files in os.walk(alternate_top, topdown=False):
        for name in files:
            if os.path.basename(name) == os.path.basename(find_file_name):
                return os.path.join(root, name)
    return ''

for root, dirs, files in os.walk(dir, topdown=False):
    for name in files:
        if name.rfind(extension) == len(name)-len(extension):
            ans = get_full_name(other_dir, name)
            if ans != '':
                print "cp -v " + os.path.join(root, name) + " " + ans
            
Text 8 May Python - Splitting a list into multiple chunks
# Lets say you have arr = [ 1, 2, ... 100, ]
# And you want chunks = [ [ 1, 2, 3, .. 10 ], [ 11, 12 .. 20 ], .. ]  ( a chunksize of 10 per chunk )

def get_chunks(arr, chunk_size = 10):
    chunks  = [ arr[start:start+chunk_size] for start in range(0, len(arr), chunk_size)]
    return chunks

arr = range(100)
print arr
print get_chunks(arr)

Alright. What just happened. If you didn’t already know read,

You can figure out the rest of it!

Text 24 Apr hello world

I am a wannabe hacker. I like to learn by doing things. Doesn’t matter if I end up repainting the bike shed. Probably, that is the desired result.

This will be a record of anything interesting I learn as I learn them.


Design crafted by Prashanth Kamalakanthan. Powered by Tumblr.