Writing to a file after parsing
<p>I have written a small python code where it will read a sample csv file and copy its first column to a temp csv file. Now when I try to compare that temporary file with another text file and try to write result to another file called result file, The file is created but with empty content.</p>

<p>But when i tested it in chunks, It is working fine</p>

<pre><code>import csv
f = open("sample.csv", "r")
reader = csv.reader(f)

data = open("temp1.csv", "w")
w = csv.writer(data)
for row in reader:
    my_row = []
    my_row.append(row[0])
    w.writerow(my_row)

with open('temp1.csv', 'r') as file1:
    with open('serialNumber.txt', 'r') as file2:
        same = set(file1).intersection(file2)

with open('result.txt', 'w') as file_out:
    for line in same:
        file_out.write(line)
        print line
</code></pre>

<p>sample.csv</p>

<blockquote>
  <p>M11435TDS144,STB#1,Router#1</p>
  
  <p>M11543TH4292,STB#2,Router#1</p>
  
  <p>M11509TD9937,STB#3,Router#1 </p>
  
  <p>M11543TH4258,STB#4,Router#1</p>
</blockquote>

<p>serialNumber.txt</p>

<blockquote>
  <p>G1A114042400571</p>
  
  <p>M11543TH4258</p>
  
  <p>M11251TH1230</p>
  
  <p>M11435TDS144</p>
  
  <p>M11543TH4292</p>
  
  <p>M11509TD9937</p>
</blockquote>

<p><strong>Points regarding code</strong>:</p>

<ul>
<li><code>data</code> file handle is not closed. <code>data.close()</code> after writing to <code>temp1.csv</code>.</li>
<li>In your code, <code>same = set(file1).intersection(file2)</code>, you are directly passing file handle <code>file2</code> to intersection. It expects list. This is exact problem is. It should be <code>same = set(file1.readlines()).intersection(file2.readlines())</code></li>
</ul>

<p><strong>Working Code:</strong></p>

<pre><code>import csv
f = open("sample.csv", "r")
reader = csv.reader(f)

data = open("temp1.csv", "wb")
w = csv.writer(data)

for row in reader:
    my_row = []
    if len(row) != 0:
      my_row.append(row[0])
      w.writerow(my_row)
#File should be closed
data.close()

with open('temp1.csv', 'r') as file1:
    with open('serialNumber.txt', 'r') as file2:
      tmp_list = (file1.readlines())
      ser_list = (file2.readlines())
      same = set(file1.readlines()).intersection(file2.readlines())


with open('result.txt', 'w') as file_out:
    for line in same:
        file_out.write(line)
</code></pre>

<p><strong>Content of temp1.csv:</strong></p>

<pre><code>M11435TDS144
M11543TH4292
M11509TD9937
M11543TH4258
</code></pre>

<p><strong>Content of result.txt :</strong></p>

<pre><code>M11543TH4258
M11543TH4292
M11435TDS144
</code></pre>

<p><em>You can use <code>with</code> for opening files <strong>sample.csv</strong> and <strong>temp1.csv</strong> as below.</em> </p>

<pre><code>import csv

with open("sample.csv") as f:
  with open("temp1.csv",'wb') as data:
    reader = csv.reader(f)
    w = csv.writer(data)
    for row in reader:
      my_row = []
      my_row.append(row[0])
      w.writerow(my_row)


with open('temp1.csv', 'r') as file1:
    with open('serialNumber.txt', 'r') as file2:
      same = set(file1.readlines()).intersection(file2.readlines())

with open('result.txt', 'w') as file_out:
    for line in same:
        file_out.write(line)
</code></pre>

python file-writing