Sybrand Brekelmans
Sybrand Brekelmans

Reputation: 15

TypeError when writing a list on a csv file

I am trying to create a code to reformat csv files and write it a list of lists on a csv file. The code is not very clean but has worked in the past. But now that I added a new first part to it, where I delete the rows and create betweenfile, it gives me a Type Error. I am unable to realize what I am doing wrong, thanks for your help.

import os
import csv  
outputfile = input("")
nodefile = input("")
inputfile =input("")
betweenfile= input("")  
os.mknod(outputfile)
os.mknod(nodefile)
os.mknod(betweenfile)   


with open(inputfile, newline='') as data:
    reader = csv.reader(data, delimiter=',')
    stack=[]    
    try:
        for row in reader:
            del row[0]
            del row[3]
            del row[3]
            del row[3]
            del row[3]
            del row[3]
            del row[4]
            row[1],row[2]=row[2],row[1]
            row[2],row[3]=row[3],row[2]
            stack.append(row)
    except:
        IndexError
    with open(betweenfile, 'w') as outputfile:
        writer= csv.writer(outputfile, delimiter=',')
        for row in stack:
            writer.writerow(row)

with open(betweenfile, newline='') as data:
    reader = csv.reader(data, delimiter=',')
    listee=[]
    for row in reader:
        if row[3]=='1':
            row[0],row[1]=row[1],row[0]
        if row[3]=='3':
            row[0],row[1]=row[1],row[0]
        row.pop(3)
        listee.append(row)
    listee.pop(0)
    for row in listee:
        for i in range(len(row)):
            if row[i]== '0': 
                listee.remove(row)
    for row in listee:
        for i in range(len(row)):
            if row[i]== '0': 
                listee.remove(row)
    print(type(listee))
    with open(outputfile, 'w') as outputfile:
        writer= csv.writer(outputfile, delimiter=',')
        writer.writerow(['from', 'to' , 'weight'])
        for row in listee:
            writer.writerow(row)

The error message is:

 Traceback (most recent call last):
  File "dataformater3.py", line 54, in <module>
    with open(outputfile, 'w') as outputfile:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper

Upvotes: 1

Views: 1020

Answers (1)

e4c5
e4c5

Reputation: 53734

This is why you should never reuse variables. They have unintended side effects. First you define outputfile as

 outputfile = input("")

Then

with open(betweenfile, 'w') as outputfile:

outputfile is a filehandle now, it's no longer the string that the user type in. And further down in your code.

print(type(listee))
with open(outputfile, 'w') as outputfile:

You are passing a file handle as the first parameter to open, which obviously doesn't know how to deal with it.

The remedy then is to avoid reusing variables. Why not borrow from .NET devs and make the following change

 outputfile_s = input("")
 ....
 with open(outputfile_s, 'w') as outputfile_f:

Upvotes: 1

Related Questions