Python子进程。带有var / args的Popen

时间:2019-02-20 11:14:41

标签: python django cmd subprocess

我想调用一个子进程。用一个变量打开cmd,但是由于某种原因它不能正常工作

@periodic_task(run_every=(crontab(minute='*/1')), name="Check Addr", ignore_result=True)
def check_user_addr():
    users = User.objects.all()
    for user in users:
        check_addr = subprocess.Popen(['My command'] + user.acc_addr , shell=True, stdout=subprocess.PIPE).communicate()[0].decode('utf-8').strip()
        print(check_addr)

那是我得到的错误:

Traceback (most recent call last):
  File "/home/user/PyCharm/MyProject/venv/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/user/PyCharm/MyProject/venv/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/user/PyCharm/MyProject/tasks.py", line 39, in check_btc_deposit
    check_addr = subprocess.Popen(['My command'] + user.acc_addr, shell=True, stdout=subprocess.PIPE).communicate()[0].decode('utf-8').strip()
TypeError: can only concatenate list (not "str") to list

感谢和亲切问候

1 个答案:

答案 0 :(得分:1)

所有args必须在类似列表中

check_addr = subprocess.Popen(['My command', user.acc_addr], shell=True, stdout=subprocess.PIPE).communicate()[0].decode('utf-8').strip()

# or for example
subprocess.run(["ls", "-l"])