Reputation: 21
I am trying to make a application to get a user to press a button and be able to open a csv file and then make a copy of that file in a certain directory, however the first line of code in the function won't work. Here is an excerpt of the code below.
import os
import pandas as pd
from tkinter import filedialog
import ttkbootstrap as tb
root = tb.Window(themename='superhero')
def add_spreadsheet():
spreadsheet_filepath = filedialog.askopenfilename(initialdir='C:\Users\peter\Downloads',
title='Select A File', filetypes=
('csv files', '*.csv'))
file_name = os.path.basename(spreadsheet_filepath)
df = pd.read_csv(spreadsheet_filepath)
output_path = os.path.join('csv files/', f'{file_name}')
df.to_csv(output_path, index=False)
# create add button to add a spreadsheet to a file
home_add_btn = tb.Button(root, text='Add', command=add_spreadsheet)
home_add_btn.pack(anchor='center', pady=10)
root.mainloop()
I tried online but I couldn't understand anything.
Upvotes: -1
Views: 108
Reputation: 3581
There are two things you have to improve:
r
or R
in front of the path name.Other options for path names, but in my opion the raw string is prefered.
filetypes
, because they are defined as a sequence of tuples, see doc.spreadsheet_filepath = filedialog.askopenfilename(initialdir= r'C:\Users\peter\Downloads', title='Select A File', defaultextension=".csv", filetypes=[("CSV Files", "*.csv")])
Here is a dummy code what opens the file dialoge:
from tkinter import *
from tkinter import filedialog
root = Tk()
def add_spreadsheet():
spreadsheet_filepath = filedialog.askopenfilename(initialdir= r'C:\Users',
title='Select A File', filetypes=
[('csv files', '*.csv')])
file_name = os.path.basename(spreadsheet_filepath)
df = pd.read_csv(spreadsheet_filepath)
output_path = os.path.join('csv files/', f'{file_name}')
df.to_csv(output_path, index=False)
# create add button to add a spreadsheet to a file
home_add_btn = Button(root, text='Add', command=add_spreadsheet)
home_add_btn.pack(anchor='center', pady=10)
root.mainloop()
Upvotes: 1
Reputation: 313
The problem you are facing is most likely a syntax error, probably like this:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
(At least that's what I get when I run your code).
The error message here states that a "unicode" error occurs. The problem with your code is the string literal of the file path in the first line of your add_spreadsheet
function. Specifically, this line:
spreadsheet_filepath = filedialog.askopenfilename(initialdir='C:\Users\peter\Downloads', title='Select A File', filetypes=('csv files', '*.csv'))
Normal strings will interpret backslashes (\
) and subsequent character(s) as something called "escape sequences". For example, the string "Hello World\nHello World again"
will be interpreted as "Hello World"
then a newline character, and then finally "Hello World again"
. Some common escape sequences include \n
(newline), \t
(tab), \r
(line feed), and \uxxx
(unicode character with ordinal number xxx). If the character after the backslash is not one of the valid escape sequences, then Python will raise a unicode error. Unfortunately, file paths are usually given with backslash characters inside. To solve this issue, there are two approaches:
\\
). This will escape the escape sequence and make it valid. Example:spreadsheet_filepath = filedialog.askopenfilename(initialdir='C:\\Users\\peter\\Downloads', title='Select A File', filetypes=('csv files', '*.csv'))
This is fine from a syntactical viewpoint, and may be the only choice if other backslashes are meant for actual escape sequences, like the newline (\n
). But if not, the second approach works better.
spreadsheet_filepath = filedialog.askopenfilename(initialdir=r'C:\Users\peter\Downloads', title='Select A File', filetypes=('csv files', '*.csv'))
This approach not only saves a lot of typing if there are many invalid escapes but also makes clear that this string is "raw". Thus, I highly recommend the latter approach. However, if you want a newline or other valid escapes then you need to use the first method.
Upvotes: 1
Reputation: 31
In strings the "" character has special properties to it. To fix your current issue just use "\\" instead...
For example:
spreadsheet_filepath = filedialog.askopenfilename(initialdir='C:\\Users\\peter\\Downloads',
title='Select A File', filetypes=
('csv files', '*.csv'))
Just replace your code with the code segment above and it should work.
Upvotes: 1