本文整理匯總了Python中subprocess.py方法的典型用法代碼示例。如果您正苦於以下問題:Python subprocess.py方法的具體用法?Python subprocess.py怎麽用?Python subprocess.py使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類subprocess
的用法示例。
在下文中一共展示了subprocess.py方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_get_console_width_no_stderr_on_failure
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import py [as 別名]
def test_get_console_width_no_stderr_on_failure():
# run docker-make with a non-console standard input
# (inspiration from Python's subprocess.py)
process = subprocess.Popen(
"docker-make devbase --repo myrepo --tag mytag".split(),
cwd=EXAMPLEDIR,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
try:
stdout, stderr = process.communicate(b"I am not a shell")
except:
process.kill()
process.wait()
raise
retcode = process.poll()
assert retcode == 0 # sanity check - did this build not work for some reason?
assert b"ioctl for device" not in stderr
示例2: check_output
# 需要導入模塊: import subprocess [as 別名]
# 或者: from subprocess import py [as 別名]
def check_output(*popenargs, timeout=None, **kwargs):
# A variation of subprocess.check_output that captures stderr and
# logs it instead of letting it go to the console directly to make
# it easier for tests to capture it.
# NOTE(dhellmann): copied from subprocess.py vv
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
if 'input' in kwargs and kwargs['input'] is None:
# Explicitly passing input=None was previously equivalent to passing an
# empty string. That is maintained here for backwards compatibility.
kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''
# NOTE(dhellmann): end copied from subprocess.py ^^
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
if 'cwd' in kwargs:
LOG.debug('cwd = {}'.format(kwargs['cwd']))
LOG.debug('$ {}'.format(' '.join(cmd)))
if 'stderr' not in kwargs:
kwargs['stderr'] = subprocess.PIPE
# Copy full environment to pass in so we can include any of our own
# environment variables with it.
env = os.environ.copy()
env_extras = kwargs.pop('env', None)
if env_extras:
env.update(env_extras)
completed = subprocess.run(*popenargs,
stdout=subprocess.PIPE,
timeout=timeout,
check=True,
env=env,
**kwargs)
if completed.stderr:
_multi_line_log(logging.WARNING, completed.stderr.decode('utf-8'))
return completed.stdout