Search code examples
pythonstringtkinterpytube

unknown string to raw string


So i have a browse button that indicates a path for a download but since it puts out a regular string it won't download since there are backslashes involved and they aren't litteraly interpreted. Edit: I added other parts of my code a bit since some parts weren't really clear.

def browse():
    global folder_path
    filename = filedialog.askdirectory()
    Path = filename
    print(Path)
BROWSEbutton = tk.Button(src, text="Browse", font="Courier 12", command=browse).place(x=425,y=0)
def Convert():
    try:   
        video = yt.YouTube(URL.get()).streams.first()
        try:
            video.download(Path)
            print("succesful")
        except:
            print("error")
            msgb.showerror("Error","Invalid Path")     
    except:
        print("error")    
        msgb.showerror("Error","Invalid URL")
CONVERTbutton = tk.Button(src, text="Convert", font="Courier 12",command=Convert).place(x=243,y=220)


Solution

    1. You are defining a global variable such as folder_path and you are not using it
    2. The path in convert() is not defined in that function where the global variable folder_path should have been used.
    3. And the path given by filedialog.askdirectory() also works for video.download()

    after removing these mistakes your code should be,

    folder_path=""
    def browse():
        global folder_path
        folder_path = filedialog.askdirectory()
        print(folder_path)
    
    def Convert():
        global folder_path
        try:   
            video = yt.YouTube(URL.get()).streams.first()
            try:
                video.download(folder_path)
                print("succesful")
            except:
                print("error")
                msgb.showerror("Error","Invalid Path")     
        except:
            print("error")    
            msgb.showerror("Error","Invalid URL")
    
    BROWSEbutton = tk.Button(src, text="Browse", font="Courier 12", command=browse).place(x=425,y=0)
    CONVERTbutton = tk.Button(src, text="Convert", font="Courier 12",command=Convert).place(x=243,y=220)
    

    hope this helps you!