title: Can't fill in the data file
body: <p>I work in django. I need to fill in the text file data from the database. The very process of searching for data in the database and change their work, and if I use print to debug, the variables are not empty. They contain data from the database.</p>
  
  <pre><code># encoding: utf-8
  # !/usr/bin/python
  
  from django.core.management.base import BaseCommand
  import os
  from core.db import KeyStore, Fingerprints
  import csv
  class Command(BaseCommand):
      help = u"Переименование файлов кейсторов и обновление данных о них в БД."
  
      def handle(self, *args, **options):
          success_csv = 'success_log.csv'
          error_csv = 'error_log.csv'
          [OUTPUT IN FILE]
          with open(success_csv, 'w') as f:
              f.write('Id;Alias;Old keystore;New keystore;\n')
          [OUTPUT IN FILE]
          with open(error_csv, 'w') as f:
              f.write('Keystore;\n')
  
          path = '/home/anton/PycharmProjects/apps-ittop-mobi/src/media/keystores'
          list_of_files = os.listdir(path)
          count_of_files = len(list_of_files)
          os.chdir(path)
          for i in range(0, count_of_files):
              old_keystore = 'keystores/%s' % list_of_files[i].encode('utf-8')
              try:
                  keystore = KeyStore.objects.get(keystore=old_keystore)
              except(ValueError, KeyStore.DoesNotExist):
                  keystore = None
                  [NOT OUTPUT IN FILE]
                  with open(error_csv, 'a') as f:
                      f.write('%s\n' % old_keystore)
              else:
                  try:
                      fingerprints = Fingerprints.objects.get(keystore_id=keystore.pk)
                  except(ValueError, Fingerprints.DoesNotExist):
                      fingerprints = None
                  else:
                      new_keystore = '%s__%s__.keystore' % (keystore.alias, keystore.keypass)
                      keystore.keystore = 'keystores/%s' % new_keystore
                      keystore.save()
                      fingerprints.save()
                      [NOT OUTPUT IN FILE]
                      with open(success_csv, 'a') as f:
                          f.write('%s;%s;%s;%s;\n' % (keystore.pk, keystore.alias, old_keystore, keystore.keystore))
  
                      os.rename(os.path.join(path, list_of_files[i]), os.path.join(path, new_keystore))
  </code></pre>
  
  <p>Even if I replace:</p>
  
  <pre><code>[NOT OUTPUT IN FILE]
  with open(error_csv, 'a') as f:
      f.write('%s\n' % old_keystore)
  </code></pre>
  
  <p>On</p>
  
  <pre><code>[NOT OUTPUT IN FILE]
  with open(error_csv, 'a') as f:
         f.write('TESTtestTEST\n')
  </code></pre>
  
  <p>In the file nothing is written. Although these sections of code work:</p>
  
  <pre><code>[OUTPUT IN FILE]
  with open(success_csv, 'w') as f:
         f.write('Id;Alias;Old keystore;New keystore;\n')
  [OUTPUT IN FILE]
         with open(error_csv, 'w') as f:
              f.write('Keystore;\n')
  </code></pre>
  
  <p>What are the reasons why the file is not populated with data?</p>