首页 > 解决方案 > python3中的子进程

问题描述

我是 python 新手,我为一个简单的子处理脚本编写了一些代码,但我无法调试解释器引发的错误。下面是我的代码;

import subprocess
import datetime as dt
import sys
import chardet

class SP():
    def execute(self, command, args=""):
        try:
            p=subprocess.Popen(command+" "+str(args),
            shell=True, stderr=subprocess.PIPE,
            stdout=subprocess.PIPE)
            print("ID of spawned process is :"+str(p.pid)+"\n")
            out,err=p.chardet.detect(out)
            out=out.decode("utf-8")
            splitted=str(out).split("\\n")
            for o in splitted:
                print(o)
        except Exception as ex:
            print("Exception caught :"+str(ex))

obj=SP()
obj.execute("ls")

这是我运行代码时遇到的错误;

Exception caught :'Popen' object has no attribute 'chardet'

请问有人可以帮忙吗?

标签: pythonsubprocess

解决方案


推荐阅读