我有以下代码,它基本上帮助我首先获取文件夹的路径,然后将工作目录设置为该特定文件夹。
我确实收到以下错误消息:
os.chdir(sourcePath) # Provide the path here
FileNotFoundError: [Errno 2] No such file or directory: 'PY_VAR0'
代码:
from tkinter import filedialog
from tkinter import *
import glob, os, shutil
def browse_button():
# Allow user to select a directory and store it in global var
# called folder_path
global folder_path
filename = filedialog.askdirectory()
folder_path.set(filename)
print(filename)
def set_dir():
sourcePath = str(folder_path)
os.chdir(sourcePath) # Provide the path here
root = Tk()
folder_path = StringVar()
lbl1 = Label(master=root,textvariable=folder_path)
lbl1.grid(row=0, column=1)
buttonBrowse = Button(text="Browse folder", command=browse_button)
buttonBrowse.grid(row=2, column=1)
buttonSetDir = Button(root, text='Set directory', command=set_dir).grid(row=2, column=2, sticky=W, pady=4)
mainloop()
答案 0 :(得分:0)
替换:
sourcePath = str(folder_path)
使用:
sourcePath = folder_path.get()
str(folder_path)
基本上获取变量的tcl解释器名称。