-1

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)

H3tR
  • 23
  • 1
  • 8
  • Better print the exception message as well. – acw1668 Jun 18 '20 at 12:04
  • 1
    The interpretation of backslashes shoudn't matter. What happens when you run your code. How do you know they aren't being handled properly? – Bryan Oakley Jun 18 '20 at 12:22
  • 1
    If your indentation here matches your actual code, then `browse()` is a completely useless function - it asks the user to select a directory, and then fails to store the user's choice in any place that will still exist after the function returns. – jasonharper Jun 18 '20 at 12:36

1 Answers1

1
  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!

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
  • **1.** Your example show the common pitfall: [AttributeError: NoneType object has no attribute](https://stackoverflow.com/a/1101765/7414759). **2.** Inside `Convert` there is no `global folder_path` needed, you read the variable. **3.** No exception type(s) specified (bare-except) is bad practice. – stovfl Jun 18 '20 at 15:57