0

I'm stuck on the best way to use a variable stored within a function to read a .csv file. I've only used the with open before using a path like (r"C:/Users\James\Desktop\test_data.csv"). If I store (r"C:/Users\James\Desktop\test_data.csv") into FilePath = (r"C:/Users\James\Desktop\test_data.csv") and use with open(FilePath) as f: it works. So I'm sure I'm messing something up within my function def select_input_file(): I have tried with open(r,(input_file_path)) as f: with open(r(input_file_path)) as f: but cant get it to work. Fyi, using python 3.8.5. I've reduced the code to include what i think is necessary for this question. Cheers

import tkinter as tk
from tkinter import *
import tkinter.ttk as tkrttk
from PIL import Image, ImageFont, ImageTk
import csv
from tkinter import filedialog

root = tk.Tk()

def select_input_file():
    global input_file_path
    input_file_path = filedialog.askopenfile(filetypes=(("CSV files", "*.csv"),))
    with open(input_file_path) as f:
        reader = csv.DictReader(f, delimiter=',')
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Import", command=select_input_file)

root.mainloop()
3
  • 1
    The r modifier only applies to string literals, because string literals have to support special escape sequences. You don't need to worry about it if you are getting your file path from somewhere else. Commented Aug 11, 2020 at 13:29
  • 2
    askopenfile() doesn't return a file path as you seem to expect, it returns an open file, which you can directly iterate over (and then explicitly .close(), since there's no way to use with in this case). Or you can change that to askopenfilename(), and leave the rest of your code as is. Commented Aug 11, 2020 at 13:32
  • great, thank you both. Commented Aug 11, 2020 at 13:45

1 Answer 1

1

The problem I believe is that you are using askopenfile instead of askopenfilename and that would supposedly solve the issue.

What askopenfile does is that it opens a file for you to read or write on to, similar to open().

But what askopenfilename does is that it returns the path of the file that you select and here it is what you want to use, as your code revolves around the path of the file.

Alternatively you could also write the code in a way that uses just the askopenfile and omit the open() and askopenfilename in your code too.

Click here for more Information on askopenfilename and example

If any doubts or errors, do let me know.

Cheers

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this askopenfilename() did the trick. However, its only reading the first line of the .csv file? has it got to do with the way the variable is stored / called?
you are using DictReader, why not reader. What is your need with the csv
To read the .csv and input to tkinter treeview I think I need to use DictReader because reader will look for integer numbers. My column names are indexed so using reader gives error TypeError
i dont believe reader would give an error. Are you sure? after using reader, you can loop through the results and get all the values
using reader = csv.reader(f, delimiter =' ', quotechar='|') and leaning on this doc docs.python.org/3/library/csv.html im getting TypeError: list indices must be integers or slices not str
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.