如何使用Python将变量导入popen命令

2024-04-18 22:37:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我做错了什么


Tags: inpyapprequestlibpackageslocalline