1

Using tkinter to select output folder for processed files.

root = tk.Tk()
root.withdraw()
file_output = filedialog.askdirectory()
x = "NAME"


file_output 
>>>'C:/Users/person/Documents/'

Would like to use file_output and x as variables for my file path, My attempt:

df.to_csv(f"{file_output}/{x} 6-30-22 11s & 12s.csv")

print((f"{file_output}/{x} 6-30-22 11s & 12s.csv"))
>>> C:/Users/person/Documents/NAME 6-30-22 11s & 12s.csv

This does not work in my program. The output I need is

C:\\Users\\person\\Documents\\NAME 6-30-22 11s & 12s.csv

I am reading the docs https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-stringprefix

Still don't understand how to escape or a workaround, Help would be much appreciated.

1 Answer 1

2

you can easily use replace to get the output you want, so try this:

df.to_csv((f"{file_output}/{x} 6-30-22 11s & 12s.csv").replace('/', '\\\\'))

print((f"{file_output}/{x} 6-30-22 11s & 12s.csv").replace('/', '\\\\'))

output:

C:\\Users\\person\\Documents\\NAME 6-30-22 11s & 12s.csv
Sign up to request clarification or add additional context in comments.

2 Comments

My output: C:\Users\person\Documents\NAME 6-30-22 11s & 12s.csv when using your answer
try 4 of '\' so use replace('\', '\\\\')

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.