title: Unable to write to binary file using pickle Python
body: <p>So I'm having this problem where I'm trying to write a dictionary to a binary file and nothing happens. Very strange; no error message, nothing. It's like the program skips over the code, specifically this code</p>
  
  <pre><code>with open("database.dat", "wb") as handle:
          pickle.dump(d, handle)
          handle.close()
  </code></pre>
  
  <p>I have no problem loading from this database.dat file if it exists but this code doesn't seem to create a new binary file if it doesn't exist and it doesn't overwrite the file if it does exist. I've never had this problem so I'm perplexed...</p>
  
  <p>To give an idea of where this fits in, this the function:</p>
  
  <pre><code>import pickle
  
  def initialise_database(directory, file_name):
  
      try:
          with open("database.dat", "rb") as handle:
              d = pickle.load(handle) # THIS WORKS PERFECTLY
              handle.close()
              return d
  
      except FileNotFoundError:
          return update_database(directory, file_name)
  
  
  def update_database(directory, file_name):
      import xlrd, os
  
      # Change directory
      os.chdir(directory)
  
      try:
          wb = xlrd.open_workbook(file_name)
  
          sheet = wb.sheet_by_name("FUNDS")
  
          rows, cols = sheet.nrows, sheet.ncols
  
          d = {}
          for i in range(rows - 1):
              if sheet.cell_value(i + 1, 0) != "":
                  charity = cap(sheet.cell_value(i + 1, 0))
  
                  area_of_work = []
                  org = []
                  funding = 0
  
                  for x in range(cols):
                      if sheet.cell_value(0, x) == "Area of work":
                          if sheet.cell_value(i + 1, x) != "":
                              area_of_work.append(cap(sheet.cell_value(i + 1, x)))
                          else:
                              pass
  
                      elif sheet.cell_value(0, x) == "Organisation funded":
                          if sheet.cell_value(i + 1, x) != "":
                              org.append(cap(sheet.cell_value(i + 1, x)))
                          else:
                              pass
  
  
                      elif sheet.cell_value(0, x) == "How much funding provided":
                          funding = (str_to_int(sheet.cell_value(i + 1, x)))
  
                          if funding is False:
                              print("\nCharity " + str(sheet.cell_value(i + 1, 0)) + ", number " + str(i + 2) + " has funding of value: "+ str(funding) + ", which is not compatible. Please find it and fix it in the the excel file. Then try again")
                              return False
  
                  # Adds entry to dictionary
                  d[charity] = [area_of_work, org, funding]
  
              else:
                  pass
  
          # After the loop has finished, it should write the entire dictionary to a newly created file!
  
          with open("database.dat", "wb") as handle:
              pickle.dump(d, handle)
              handle.close()
  
          return d
  
      except FileNotFoundError:
          print("File could not be found.")
          print("Program terminating.\n\nPress the enter key to exit.")
  </code></pre>
  
  <p>I've taken a look at other questions and none seem to touch on the problem I'm facing here. Any help on how to fix it would be greatly appreciated!</p>
  
  <p>Edit:
  I'd also like to add that when I run the code and perform</p>
  
  <pre><code>print(initialise_database( #my_directory, #my_file )
  </code></pre>
  
  <p>It does print out my database, meaning that the update() function is being called, seeing as there is no such database.dat file anywhere</p>
  
  <p>As proof that the file is not appearing, here is a screenshot. This is after full_script (which is where this code is taken from) has been ran.</p>
  
  <p><a href="https://i.stack.imgur.com/LZy4O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LZy4O.png" alt="Evidence there is no file"></a></p>