我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.popen3()。
def popen3(cmd, mode="t", bufsize=-1): """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If 'cmd' is a string it will be passed to the shell (as with os.system()). If 'bufsize' is specified, it sets the buffer size for the I/O pipes. The file objects (child_stdin, child_stdout, child_stderr) are returned.""" import warnings msg = "os.popen3 is deprecated. Use the subprocess module." warnings.warn(msg, DeprecationWarning, stacklevel=2) import subprocess PIPE = subprocess.PIPE p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring), bufsize=bufsize, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) return p.stdin, p.stdout, p.stderr
def run_command(self, command, check_output=True): """Run a command for this collectd plugin. Returns a tuple with command success and output or False and None for output. """ output = None try: if check_output: output = subprocess.check_output(command) else: stdin, stdout, stderr = os.popen3(' '.join(command)) output = stdout.read() except Exception as exc: collectd.error( 'collectd-ceph-storage: {} exception: {}'.format(command, exc)) collectd.error( 'collectd-ceph-storage: {} traceback: {}' .format(command, traceback.format_exc())) return False, None if output is None: collectd.error( 'collectd-ceph-storage: failed to {}: output is None' .format(command)) return False, None return True, output
def run_test(script, cmdline_rest=""): dirname, scriptname = os.path.split(script) # some tests prefer to be run from their directory. cwd = os.getcwd() os.chdir(dirname) try: executable = win32api.GetShortPathName(sys.executable) cmd = '%s "%s" %s' % (sys.executable, scriptname, cmdline_rest) print script stdin, stdout, stderr = os.popen3(cmd) stdin.close() while 1: char = stderr.read(1) if not char: break sys.stdout.write(char) for line in stdout.readlines(): print line stdout.close() result = stderr.close() if result is not None: print "****** %s failed: %s" % (script, result) finally: os.chdir(cwd)
def wrap_popen3_for_win(cygwin_path): """Wrap popen3 to support #!-script on Windows. Args: cygwin_path: path for cygwin binary if command path is needed to be translated. None if no translation required. """ __orig_popen3 = os.popen3 def __wrap_popen3(cmd, mode='t', bufsize=-1): cmdline = cmd.split(' ') interp = get_script_interp(cmdline[0], cygwin_path) if interp: cmd = interp + ' ' + cmd return __orig_popen3(cmd, mode, bufsize) os.popen3 = __wrap_popen3
def get_command_output(cmd, and_stderr=False): """ Return a pipe from which a command's output can be read. cmd is the command. and_stderr is set if the output should include stderr as well as stdout. """ try: import subprocess except ImportError: if and_stderr: _, sout = os.popen4(cmd) else: _, sout, _ = os.popen3(cmd) return sout if and_stderr: stderr = subprocess.STDOUT else: stderr = subprocess.PIPE p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr) return p.stdout