-1

This is the script

from tkinter import *
import os

window = Tk()
window.title("Download from Web")
window.geometry("300x120+200+100")

photo = PhotoImage(file = "icon/icon.png")
window.iconphoto(False, photo)


urltxt = Label(window, text = "Url").place(x = 10, y = 10)  
url = Entry(window, width=30)
url.insert(0,"")
url.grid(row=1, column=0, padx=80, pady=10)


filetxt = Label(window, text = "Save File name").place(x = 10, y = 10)  
filename = Entry(window, width=30)
filename.insert(0,".exe,.zip,.rar")
filename.grid(row=2, column=0, padx=80, pady=10)



def Message():
    os.system(f"powershell -c "Invoke-WebRequest -Uri '{url.get()}' -OutFile '{filename.get()}'"")

btnSendMessage = Button(window, text="Search", width=20, command=Message)
btnSendMessage.grid(row=4, column=0, padx=10, pady=10)

window.mainloop()

I'm having a problem with this part of the text

def Message():
    os.system(f"powershell -c "Invoke-WebRequest -Uri '{url.get()}' -OutFile '{filename.get()}'"")

I'm having a problem with these two parts in particular when I add the ' to the code I get an error

'{url.get()}' and '{filename.get()}'
123
  • 3
  • 4
  • The single and double quotes can be escaped with `\'` and `\"`, respectively – Andrew Mascillaro Oct 11 '21 at 14:00
  • Do you mean like this `'\{url.get()}'` – 123 Oct 11 '21 at 14:07
  • In general, using `os.system()` is a bad idea. The `subprocess` module can let you pass an argv array so you don't need to figure out how to construct an appropriately-quoted string. – Charles Duffy Oct 11 '21 at 14:21
  • Also: Which platform is this for? (Powershell is available on UNIX platforms, so that doesn't tell us anything conclusively; but what kind of quoting needs to be used for `os.system` differs between UNIX and Windows) – Charles Duffy Oct 11 '21 at 14:23

1 Answers1

0

Quotes can be escaped with backslashes as shown:

os.system(f"powershell -c \"Invoke-WebRequest -Uri \'{url.get()}\' -OutFile \'{filename.get()}\'\"")