Windows to Linux script issues: "IndexError: list index out of range"

I have a script – works perfectly in Windows, but when I tried running it in Ubuntu it spat out the error message:

IndexError: list index out of range.

It’s a pretty simple script: it imports a CSV file, reads the rows, prints the first item in each row into a list, uses set() to remove duplicates, then writes this new list to a file.

import csv, glob

for x in glob.glob("*raw_vcf.csv"):
   csv_f = open(x, "r")

data = [c for c in csv.reader(csv_f)]
frags_unique = []

def frag_list(vcf_data, uniquefrags):
    """ 
    User input: an imported .vcf file (='vcf_import'); an empty list
    (= 'uniquefrags').
    'frag_list' takes 'vcf_import', reads each row/list, taking the first item
    and attaching only unique values to 'uniquefrags', using the set() function.
    First row (header row) in 'vcf_data' is deleted; not needed.
    """
    del vcf_data[0]
    list_1 = []
    for row in vcf_data:
        list_1.append(row[0])
    for item in list(set(list_1)):
        uniquefrags.append(item)

frag_list(data, frags_unique)

out = open("output_unique_frags.txt","w")
for frags in frags_unique:
    out.write(frags+"n")
out.close()

Specifically, the error occurs in the module:

Traceback (most recent call last):
  File "PRIME_unique_frags.py", line 50, in <module>
    frag_list(data, frags_unique)
  File "PRIME_unique_frags.py", line 46, in frag_list
    list_1.append(row[0])
IndexError: list index out of range

But I honestly can’t see what is wrong with it, given it works on my Windows OS; tried rewriting it in different ways but no luck.

Some sample input data (“*_raw_vcf.csv”):

A,B,C,D,E
1,2,3,4,5
1,5,4,3,2
2,3,4,5,6
2,3,4,7,8
3,4,5,6,7

Should in theory (and does in Windows) produce a file (“output_unique_frags.txt”; the unique values in column A):

1
2
3


Source: ubuntu

Leave a Reply