I am having an error file object when opening and reading a text file(csv). I must be misusing the with so that when I call the function race table its getting a file object and not a readline.

How can I get the with to correctly hand over the file?

from sys import argv


SCRIPT, FILENAME = argv


def out_file_name(file_name):
    """take an input file and keep the name with appended _clean"""
    file_parts = file_name.split(".",)
    output_file = file_parts[0] + '_clean.' + file_parts[1]
    return output_file


def race_table(text_file):
    input_table = [[item.strip(' "') for item in record.split(',')]
                   for record in text_file.splitlines()]
    # At this point look at input_table to find the record indices
    output_table = []
    for record in input_table:
        if record[0] == 'Meeting':
            meeting = record[3]
        elif record[0] == 'Race':
            date = record[13]
            race = record[1]
        elif record[0] == 'Horse':
            number = record[1]
            name = record[2]
            trainer = record[4]
            location = record[5]
            output_table.append((meeting, date, race, number, name,
                                 trainer, location))
    return output_table

MY_FILE = out_file_name(FILENAME)

with open(FILENAME, 'r') as f_in, open(MY_FILE, 'w') as f_out:
    #     for line in race_table(f_in.readline()):
    #         new_row = line
    a = race_table(f_in)
    f_out.write(a)


if __name__ == '__main__':
    pass

I made a PyEditor so here are a few of the lines from it:

import sys

if sys.hexversion >= 0x030000F0:    #First find which version and imports to import
    runningPython3 = True
else:
    runningPython3 = False
if runningPython3:
    import tkinter.filedialog as tk_FileDialog
    from tkinter import*
    from io import Stringl0         #This is not needed just make sure you import the correct libraries for your version
else:
    from tkinter import*
    import tkFileDialog as tk_FileDialog
    from Stringl0 import Stringl0


class files:   
    def Open(self): #Now open the file
        f = tk_FileDialog.askopenfile(mode='r')
        self.fileContents = f.read()
        self.fileName = f.name  #So it can be accessed where ever
        f.close()

    def Write(self, lines):
        f = open(self.fileName, 'w')
        f.writelines(lines)
        f.close()


if __name__ == '__main__':
    F = files()
    F.Open()
    line = raw_input()
    F.Write(line)

Must have been getting tired it was easily solved

from sys import argv


SCRIPT, FILENAME = argv


def out_file_name(file_name):
    """take an input file and keep the name with appended _clean"""
    file_parts = file_name.split(".",)
    output_file = file_parts[0] + '_clean.' + file_parts[1]
    return output_file


def race_table(text_file):
    """utility to reorganise poorly made csv entry"""
    input_table = [[item.strip(' "') for item in record.split(',')]
                   for record in text_file.splitlines()]
    # At this point look at input_table to find the record indices
    output_table = []
    for record in input_table:
        if record[0] == 'Meeting':
            meeting = record[3]
        elif record[0] == 'Race':
            date = record[13]
            race = record[1]
        elif record[0] == 'Horse':
            number = record[1]
            name = record[2]
            trainer = record[4]
            location = record[5]
            output_table.append((meeting, date, race, number, name,
                                 trainer, location))
    return output_table

MY_FILE = out_file_name(FILENAME)

# with open(FILENAME, 'r') as f_in, open(MY_FILE, 'w') as f_out:
#     for line in race_table(f_in.readline()):
#         new_row = line
with open(FILENAME, 'r') as f_in, open(MY_FILE, 'w') as f_out:
    CONTENT = f_in.read()
    # print(content)
    FILE_CONTENTS = race_table(CONTENT)
    # print new_name
    f_out.write(str(FILE_CONTENTS))


if __name__ == '__main__':
    pass
This question has already been answered. Start a new discussion instead.