我可以在我的 powershell 命令中放置一个变量吗? (在 python 中)
Can I put a variable in my powershell command? (in python)
我正在使用
os.system('powershell.exe "Invoke-WebRequest -Uri Link')
我希望你能使 link 成为我已经定义的变量。
我的整个代码看起来像这样
import os
link=input("What link do you want? ")
os.system('powershell.exe "Invoke-WebRequest -Uri link')
但我不知道如何(如果可能的话)将变量放在那里?
您可以简单地定义一个变量来保存您想要 运行 的整个字符串。
input_var = str(input("Write -> "))
basic_command = "'powershell.exe " + '"Invoke-WebRequest -Uri '
full_command = basic_command + input_var + '"' + "'"
os.system(full_command)
在input_var中我们将输入转换为字符串。
在基本命令中,我们将您想要的命令的第一部分设置为 运行。
在 full_command 中,我们添加 input_var 然后我们用适当的 " ' close 整行。
然后我们 运行 os 带有完整命令行的系统。
希望对您有所帮助。我在很多脚本中都使用了这个概念
在Python
中使用格式化字符串的解决方案
import os
link = input("What link do you want? ")
os.system('powershell.exe "Invoke-WebRequest -Uri {link}'.format(link = link))
我正在使用
os.system('powershell.exe "Invoke-WebRequest -Uri Link')
我希望你能使 link 成为我已经定义的变量。 我的整个代码看起来像这样
import os
link=input("What link do you want? ")
os.system('powershell.exe "Invoke-WebRequest -Uri link')
但我不知道如何(如果可能的话)将变量放在那里?
您可以简单地定义一个变量来保存您想要 运行 的整个字符串。
input_var = str(input("Write -> "))
basic_command = "'powershell.exe " + '"Invoke-WebRequest -Uri '
full_command = basic_command + input_var + '"' + "'"
os.system(full_command)
在input_var中我们将输入转换为字符串。 在基本命令中,我们将您想要的命令的第一部分设置为 运行。 在 full_command 中,我们添加 input_var 然后我们用适当的 " ' close 整行。 然后我们 运行 os 带有完整命令行的系统。
希望对您有所帮助。我在很多脚本中都使用了这个概念
在Python
中使用格式化字符串的解决方案import os
link = input("What link do you want? ")
os.system('powershell.exe "Invoke-WebRequest -Uri {link}'.format(link = link))