how to delete entire row in csv file and save changes on same file?
<p>i'm new with python and try to modify csv file so i will able to delete specific rows with specific fields according to given list.
in my current code i get the rows which i want to delete but i can't delete it and save the changes on same file (replace). </p>

<pre><code> import os, sys, glob
 import time ,csv
 # Open a file
 path = 'C:\\Users\\tzahi.k\\Desktop\\netzer\\'
 dirs = os.listdir( path )
 fileslst = []
 alertsCode = ("42001", "42003", "42006","51001" , "51002" ,"61001" ,"61002","71001",
          "71002","71003","71004","71005","71006","72001","72002","72003","72004",
          "82001","82002","82003","82004","82005","82006","82007","83001","84001")
 # This would print the unnesscery codes 
 for file in dirs:
    if "ALERTS" in file.upper()  :
       fileslst.append(file)
fileslst.sort()

with open(fileslst[-1], 'rb') as csvfile:
    csvReader = csv.reader(csvfile)
    for row in csvReader:
         for alert in alertsCode:
             if any(alert in row[2] for s in alertsCode) :
             print row
</code></pre>

<p>any help? </p>

<p>Read all the rows into a list using a <em>list comprehension</em> and excluding the unwanted rows. Then <em>rewrite</em> the rows to the file in mode <code>w</code> (write mode) which overwrites or replaces the content of the file:</p>

<pre><code>with open(fileslst[-1], 'rb') as csvfile:
    csvReader = csv.reader(csvfile)
    clean_rows = [row for row in csvReader if not any(alert in row[2] for alert in alertsCode)]
    # csvfile.truncate()

with open(fileslst[-1], 'wb') as csvfile:
    csv_writer = csv.writer(csvfile)
    csv_writer.writerows(clean_rows)
</code></pre>

python csv