title: How To Delete A csv file upon Exit of Python Operation
body: <p>I'm trying to make a Python script that reads from a csv file. Upon exit of the Python script or when the Python program is killed or terminated, I want it to delete the csv file that it was reading. How do I do that? Here is my code.</p>
  
  <pre><code>import numpy as np
  import csv
  
  count=1
  
  while (count&gt;0):
      csv=np.genfromtxt("csvdata.csv", delimiter=",", filling_values=np.nan, dtype=int, case_sensitive=True, deletechars='', replace_space=' ')
      ref=np.genfromtxt("refdata.csv", delimiter=",", filling_values=np.nan, dtype=int, case_sensitive=True, deletechars='', replace_space=' ')
      A=np.array(csv)
      B=np.array(ref)
      D = B - A[-1]
      match= B[np.abs(D).sum(axis=1).argmin()]
      print match
      count = count + 1
  </code></pre>
  
  <p>I want to delete 'csvdata.csv' upon exit of the Python program. But the codeit has to be in this Python script so it doesn't have to involve manual deletion of the csv. (It is so, because the csv is created repeatedly by another Python script aside from this one)</p>
  
  <p>All help will be appreciated. Thank you very much.</p>
  
  <p>edit:</p>
  
  <p>I tried using os to delete the file but it won't work. I also tried changing the paths specified with the <code>rm</code>.</p>
  
  <pre><code>import numpy as np
  import csv
  import os
  
  count=1
  
  while (count&gt;0):
      csv=np.genfromtxt("csvdata.csv", delimiter=",", filling_values=np.nan, dtype=int, case_sensitive=True, deletechars='', replace_space=' ')
      ref=np.genfromtxt("refdata.csv", delimiter=",", filling_values=np.nan, dtype=int, case_sensitive=True, deletechars='', replace_space=' ')
      A=np.array(csv)
      B=np.array(ref)
      D = B - A[-1]
      match= B[np.abs(D).sum(axis=1).argmin()]
      print match
      count = count + 1
  
  os.system('rm csvdata.csv')
  </code></pre>