How do I use multiple variables and strings within an os.system() command?

Question:

im trying to make a simple program that downloads a file. im having a problem with the command part. here is the code:

import os

#gather user input
print("hello! welcome to the website dowloader! paste in the url(including the http 
part) and type in the file name!)")
url = input("website url: ")
filename = input("the filename:")

#the command i want run. for example, if the url was "https://example.com" and the 
#filename was "example.html"
#then i would want the command run to be: 'curl https://example.com --output 
#example.html'
cmd = str("curl ", url," --output ", filename)
os.system(cmd)
Asked By: Westin Francis

||

Answers:

str("curl ", url," --output ", filename) are you asking how to concatenate strings? You do that with the + operator, but usually, formatting strings would be eaiser here, so just f"curl {url} --output {filename}". Also, you should probably be using subprocess instead of os.system

answer by @juanpa.arrivillaga

Answered By: Westin Francis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.