how to remove rows and count it in same file opening
<p>I am trying to open CSV file and remove some rows from it according to given list, on same progress I want to count those rows for future usage. </p>

<p>Each file opening works great on separately, how can I combine them on same open file progress? </p>

<p>My current code is : </p>

<pre><code># read latest file and count rows  

with open(fileslst[-1], 'rb') as csvfile:
csvReader = csv.reader(csvfile)
for row in csvReader:
    for alert in alertsCode:
        if any(alert in row[11] for s in alertsCode) :
            counter=counter+1
print (str(counter)+" unnecessary codes in this file")


#open and modify the latest file

with open(fileslst[-1], 'rb') as csvfile:
    csvReader = csv.reader(csvfile)
    clean_rows = [row for row in csvReader if not any(alert in row[11] for alert in alertsCode)]

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

<p>When I run it on same script it's not working but run separately will. </p>

<p>What am I doing wrong? </p>

<p>according to your initial description of the problem this could be one solution, although it can surely be optimized</p>

<pre><code>excluded = ['your excluded list here']
deleted_rows = 0
with open(file) as read:
    with open(file2) as write:
        w = csv.writer(write)
        r = csv.reader(read)
        for row in r:
            if row not in excluded:
                w.writerow(row)
            else:
                deleted_rows += 1
</code></pre>

python csv