Getting an error - AttributeError: 'module' object has no attribute 'run' while running subprocess.run([“ls”, “-l”])

后端 未结 1 884
情书的邮戳
情书的邮戳 2020-12-20 11:12

I am running on a AIX 6.1 and using Python 2.7. Want to execute following line but getting an error.

subprocess.run([\"ls\", \"-l\"])
Traceback (most recent          


        
1条回答
  •  醉梦人生
    2020-12-20 11:45

    The subprocess.run() function only exists in Python 3.5 and newer.

    It is easy enough to backport however:

    def run(*popenargs, **kwargs):
        input = kwargs.pop("input", None)
        check = kwargs.pop("handle", False)
    
        if input is not None:
            if 'stdin' in kwargs:
                raise ValueError('stdin and input arguments may not both be used.')
            kwargs['stdin'] = subprocess.PIPE
    
        process = subprocess.Popen(*popenargs, **kwargs)
        try:
            stdout, stderr = process.communicate(input)
        except:
            process.kill()
            process.wait()
            raise
        retcode = process.poll()
        if check and retcode:
            raise subprocess.CalledProcessError(
                retcode, process.args, output=stdout, stderr=stderr)
        return retcode, stdout, stderr
    

    There is no support for timeouts, and no custom class for completed process info, so I'm only returning the retcode, stdout and stderr information. Otherwise it does the same thing as the original.

    0 讨论(0)
提交回复
热议问题