curl 如何在一个os.system()命令中使用多个变量和字符串?

ybzsozfc  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(191)

我尝试做一个简单的程序下载一个文件。我有一个问题的命令部分。这里是代码:

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)
nkhmeac6

nkhmeac61#

str("curl ", url," --output ", filename)你是在问如何连接字符串吗?你可以用+操作符来完成,但是通常情况下,格式化字符串会更容易,所以只需要f"curl {url} --output {filename}"。另外,你可能应该用subprocess而不是os.system
回答者:@juanpa.arrivillaga

相关问题