Wordlist Generator. Split file Size. How?
Question: I am trying to get this python script to create a new file and continue
generating word combinations once a certain file size is reached.

    
    
    f=open('wordlist', 'w')
    
    def xselections(items, n):
        if n==0: yield []
        else:
            for i in xrange(len(items)):
                for ss in xselections(items, n-1):
                    yield [items[i]]+ss
    
    # Numbers = 48 - 57
    # Capital = 65 - 90
    # Lower = 97 - 122
    numb = range(48,58)
    cap = range(65,91)
    low = range(97,123)
    choice = 0
    while int(choice) not in range(1,8):
        choice = raw_input('''
        1) Numbers
        2) Capital Letters
        3) Lowercase Letters
        4) Numbers + Capital Letters
        5) Numbers + Lowercase Letters
        6) Numbers + Capital Letters + Lowercase Letters
        7) Capital Letters + Lowercase Letters
        : ''')
    
    choice = int(choice)
    poss = []
    if choice == 1:
        poss += numb
    elif choice == 2:
        poss += cap
    elif choice == 3:
        poss += low
    elif choice == 4:
        poss += numb
        poss += cap
    elif choice == 5:
        poss += numb
        poss += low
    elif choice == 6:
        poss += numb
        poss += cap
        poss += low
    elif choice == 7:
        poss += cap
        poss += low
    
    bigList = []
    for i in poss:
        bigList.append(str(chr(i)))
    
    MIN = raw_input("What is the min size of the word? ")
    MIN = int(MIN)
    MAX = raw_input("What is the max size of the word? ")
    MAX = int(MAX)
    for i in range(MIN,MAX+1):
        for s in xselections(bigList,i): f.write(''.join(s) + '\n')
    


Answer: You can encapsulate the file rotation behavior in a class. When you are
writing some data, the write method will first check if the write would exceed
the file size limit; then it calls the rotate method which closes the current
file and opens a new one, incrementing the sequence number on the filename:

    
    
    import os
    
    class LimitWriter(object):
    
        def __init__(self, basepath, bytelimit):
            self._basepath = basepath
            self._bytelimit = bytelimit
            self._sequence = 0
            self._output = None
            self._bytes = 0
            self._rotate()
    
        def _rotate(self):
            if self._output:
                self._output.close()
            path = '%s.%06d' % (self._basepath, self._sequence)
            self._output = open(path, 'wb')
            self._bytes = 0
            self._sequence += 1
    
        def write(self, data):
            size = len(data)
            if (self._bytes + size) > self._bytelimit:
                self._rotate()
            self._bytes += size
            self._output.write(data)
    
    out = LimitWriter('wordlist', 1024 * 1024 * 1)
    
    for i in range(MIN,MAX+1):
        for s in xselections(bigList,i):
            out.write(''.join(s) + '\n')
    

Would output a series of files which are smaller than 1MB:

    
    
    1.0M  wordlist.000000
    1.0M  wordlist.000001
    252K  wordlist.000002
    

**Update** \- A few more tips on using some of the built-in power of Python to
help make your code a bit shorter and easier to follow. I've included comments
explaining each part.

Here are the docs on the modules I use below:
[itertools](http://docs.python.org/library/itertools.html),
[string](http://docs.python.org/library/string.html).

    
    
    import itertools
    import os
    from string import digits, lowercase, uppercase
    
    # PUT LimitWriter CLASS DEFINITION HERE
    
    LIMIT = 1024 * 1024 * 1
    
    choice = 0
    while int(choice) not in range(1,8):
        choice = raw_input('''
        1) Numbers
        2) Capital Letters
        3) Lowercase Letters
        4) Numbers + Capital Letters
        5) Numbers + Lowercase Letters
        6) Numbers + Capital Letters + Lowercase Letters
        7) Capital Letters + Lowercase Letters
        : ''')
    
    MIN = int(raw_input("What is the min size of the word? "))
    MAX = int(raw_input("What is the max size of the word? "))
    
    # replace your ranges and large if/else with this
    choices = {
        1: digits,
        2: uppercase,
        3: lowercase,
        4: uppercase + lowercase,
        5: digits + lowercase,
        6: digits + uppercase + lowercase,
        7: uppercase + lowercase
        }
    
    # pick one of the sets with the user's choice
    chars = choices[int(choice)]
    
    out = LimitWriter('wordlist', LIMIT)
    
    # generate all permutations of the characters from min to max
    for length in range(MIN, MAX+1):
        for tmp in itertools.permutations(chars, length):
            out.write(''.join(tmp) + '\n')
    

