'os.chdir() not working due to awaiting user input

I have a .get() function to accept user input of a path link. Then use the user's input to change directory @ os.chrdir. But running the code gives me error as this:

os.chdir(bomlist) OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''

Anyway of working around this problem?

Here is the code.

import tkinter as tk , os
entry1 = tk.Entry(top, bd=5, width = 40)
bomlist = entry1.get()
os.chdir(bomlist) #error here
wb = openpyxl.load_workbook('excel.xlsx', data_only= True)



Solution 1:[1]

If you are using tkinter then I would recommend opening the file using the from tkinter.filedialog import askopenfilename

So your code should be

path = askopenfilename()
wb = openpyxl.load_workbook(path, data_only= True)

It you want to trigger the opening function you can use a button so when the user clicks a button only then the dialog box pops up

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vivek Singh