如何使用Python将变量传递到popen命令中

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

我当前的问题是,我正在使用python和HTML创建一个应用程序,以使用HTML表单来收集数据,然后将其转换为Python中的变量,然后使用子流程POPEN将Azure CLI命令发送给Azure租户。当我对命令使用以下代码时,它返回错误。

Python代码

@app.route('/storageaccountcreate', methods = ['POST'])
def storageaccountcreate():
    name = request.form['storageaccountname']
    resourcegroup = request.form['resourcegroup']
    subscription = request.form['subscription']
    location = request.form['location']
    sku = request.form['sku']
    cmd = f"az storage account create -n {name} -g {resourcegroup} --subscription {subscription} -l {location} --sku {sku}"

    #This is where the command is initiated using subprocess
    command = Popen(cmd)
    text = command.stdout.read().decode("ascii") 
    print(text)
    with open("file.txt","w") as f:
        f.write(text)
    return (cmd)

错误

[2020-04-06 19:36:46,828] ERROR in app: Exception on /storageaccountcreate [POST]
Traceback (most recent call last):
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "c:\Users\PP284QZ\Desktop\repo\ms-identity-python-webapp-master\app.py", line 67, in storageaccountcreate
    command = Popen(cmd)
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
  File "c:\Users\PP284QZ\.vscode\extensions\ms-python.python-2020.3.71659\pythonFiles\lib\python\debugpy\no_wheels\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 631, in new_CreateProcess
    return getattr(_subprocess, original_name)(app_name, cmd_line, *args)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我在这里做错了什么?

python bash azure subprocess command-line-interface
1个回答
0
投票

您可以如下更改Popen的代码:

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