title: Python 3.2 File I/O. Generating & Spliting a file based on preset size limit. How?
body: <p>I'm working on some code that generates all permutations of a given set of characters. Although my code works in Python 2.7 it no longer does in Python 3.x due to many changes in Strings. I'd like to adapt and since I'm new to Python, I was hoping you could give me a little push. =)</p>
  
  <p><strong>My question is</strong>, as Python generates the word list of your choice, the output file size grows accordingly. I'd like you to show me how can I make this script check for a preset file size and if reached, open a new file and continue writing permutations.</p>
  
  <p>Example:</p>
  
  <p>numeric_lowercase.000001<br/>
  numeric_lowercase.000002<br/>
  numeric_lowercase.000003<br/></p>
  
  <p><strong>Remember</strong>, I have looked at most examples on the site but they do not work with Python 3.2.</p>
  
  <p>Here's my Python 3.2 working code so far:</p>
  
  <pre><code>import itertools
  import subprocess
  import os
  from string import digits, ascii_lowercase, ascii_uppercase, punctuation
  
  if os.name == 'nt':
      def clear_console():
          subprocess.call("cls", shell=True)
          return
  else:
      def clear_console():
          subprocess.call("clear", shell=True)
          return
  
  def generate_phone_numbers(area_code):
      f = open('phones.txt', 'w')
      for i in range(2010000, 9999999):
          f.write(area_code + str(i) + '\n')
  
  def generate_wordlist(lst_chars, min_digit, max_digit, lst_name):
      f = open(lst_name, 'w')
      for curr_length in range(min_digit, max_digit + 1):
          for curr_digit in itertools.product(lst_chars, repeat=curr_length):
              f.write(''.join(curr_digit) + '\n')
  
  print ('')
  print ('  wgen - Menu')
  
  choice = 0
  
  while int(choice) not in range(1,6):
      clear_console()
      choice = input('''
    1. Phone numbers for a given area code.
    2. Numbers.
    3. Numbers + Lowercase.
    4. Numbers + Lowercase + Uppercase.
    5. Numbers + Lowercase + Uppercase + Punctuation.
  
    Enter Option: ''')
  
  print ('')
  
  choice = int(choice)
  
  if choice == 1:
      area_code = input('''
    Please enter Area Code: ''')
      area_code = str(area_code)
      area_code = area_code.strip()
      if len(area_code) == 3:
          print ('')
          print ('  Generating phone numbers for area code ' + area_code + '.')
          print ('  Please wait...')
          generate_phone_numbers(area_code)
  
  if choice == 2:
      min_digit = input('  What is the minimum size of the word? ')
      min_digit = int(min_digit)
      print ('')
      max_digit = input('  What is the maximum size of the word? ')
      max_digit = int(max_digit)
      chars = digits
      lst_name = 'numeric.txt'
      print ('')
      print ('  Generating numbers between ' + str(min_digit) + ' and ' + str(max_digit) + ' digits.')
      print ('  Please wait...')
      generate_wordlist(chars, min_digit, max_digit, lst_name)
  
  if choice == 3:
      min_digit = input('  What is the minimum size of the word? ')
      min_digit = int(min_digit)
      print ('')
      max_digit = input('  What is the maximum size of the word? ')
      max_digit = int(max_digit)
      chars = digits + ascii_lowercase
      lst_name = 'numeric_lowercase.txt'
      print ('')
      print ('  Generating numbers &amp; lowercase between ' + str(min_digit) + ' and ' + str(max_digit) + ' digits.')
      print ('  Please wait...')
      generate_wordlist(chars, min_digit, max_digit, lst_name)
  
  if choice == 4:
      min_digit = input('  What is the minimum size of the word? ')
      min_digit = int(min_digit)
      print ('')
      max_digit = input('  What is the maximum size of the word? ')
      max_digit = int(max_digit)
      chars = digits + ascii_lowercase + ascii_uppercase
      lst_name = 'numeric_lowercase_uppercase.txt'
      print ('')
      print ('  Generating numbers, lowercase &amp; uppercase between ' + str(min_digit) + ' and ' + str(max_digit) + ' digits.')
      print ('  Please wait...')
      generate_wordlist(chars, min_digit, max_digit, lst_name)
  
  if choice == 5:
      min_digit = input('  What is the minimum size of the word? ')
      min_digit = int(min_digit)
      print ('')
      max_digit = input('  What is the maximum size of the word? ')
      max_digit = int(max_digit)
      chars = punctuation
      lst_name = 'numeric_lowercase_uppercase_punctuation.txt'
      print ('')
      print ('  Generating numbers, lowercase, uppercase &amp; punctuation between ' + str(min_digit) + ' and ' + str(max_digit) + ' digits.')
      print ('  Please wait...')
      generate_wordlist(chars, min_digit, max_digit, lst_name)
  </code></pre>