title: How to delete a file ONLY AFTER it has been downloaded from google colaboratory
body: <p>I am trying to download a file from google colab and delete it after downloading it.</p>
  <p>This is my code</p>
  <pre><code>from google.colab import files
  import os
  import numpy as np
  
  
  dir_to_zip = 'FOLDER_NAME'
  
  os.mkdir('/content/' + dir_to_zip)
  os.chdir('/content/' + dir_to_zip)
  
  
  ################################################################################
  # MAKING FILES
  np.savetxt('a.csv', np.array([3, 5, 6, 76]), delimiter=&quot;,&quot;)
  np.savetxt('b.csv', np.array([3, 590, 6, 76]), delimiter=&quot;,&quot;)
  
  ################################################################################
  os.chdir('/content')
  output_filename = dir_to_zip + '.zip'
  print('output', output_filename)
  os.system( &quot;zip -r {} {}&quot;.format(output_filename, dir_to_zip))
  
  files.download(output_filename)
  os.system(&quot;rm -r {}&quot;.format(dir_to_zip))
  os.system(&quot;rm -r {}&quot;.format(output_filename))
  </code></pre>
  <p>In the code I am deleting the <code>.zip</code> file after downloading it, but it is not being downloaded. and I am getting this</p>
  <p><a href="https://i.stack.imgur.com/z1aQn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z1aQn.png" alt="enter image description here" /></a></p>
  <p>Colab deleted the file before downloading/fully downloaded it even though downloading happens before deleting in code.</p>
  <p>When I try to do it as 2 cells and run both one after the other, colab deletes the file before downloading and I get the same results.</p>
  <p>cell 1:</p>
  <pre><code>from google.colab import files
  import os
  import numpy as np
  
  
  dir_to_zip = 'FOLDER_NAME'
  
  os.mkdir('/content/' + dir_to_zip)
  os.chdir('/content/' + dir_to_zip)
  
  
  ################################################################################
  # MAKING FILES
  np.savetxt('a.csv', np.array([3, 5, 6, 76]), delimiter=&quot;,&quot;)
  np.savetxt('b.csv', np.array([3, 590, 6, 76]), delimiter=&quot;,&quot;)
  
  ################################################################################
  os.chdir('/content')
  output_filename = dir_to_zip + '.zip'
  print('output', output_filename)
  os.system( &quot;zip -r {} {}&quot;.format(output_filename, dir_to_zip))
  
  files.download(output_filename)
  os.system(&quot;rm -r {}&quot;.format(dir_to_zip))
  </code></pre>
  <p>cell 2:</p>
  <pre><code>os.system(&quot;rm -r {}&quot;.format(output_filename))
  </code></pre>
  <p>It is possible to WAIT FOR SOME TIME and run the second cell after downloading the file but is there an automatic way?</p>
  <p>Is there a way to make colab delete a file only after it has downloaded and make it automatic instead of me waiting for it to be completely downloaded?</p>
  <p>Thanks</p>