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

问题描述 投票:0回答:1

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

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)
python curl os.system
1个回答
0
投票

str("curl ", url," --output ", filename)
您是在问如何连接字符串吗?您可以使用
+
运算符来完成此操作,但通常,在这里格式化字符串会更容易,因此只需使用
f"curl {url} --output {filename}"
即可。另外,您可能应该使用
subprocess
而不是
os.system

© www.soinside.com 2019 - 2024. All rights reserved.