SimpleDao
Python执行shell命令
2019-05-06, 访问数: 878

popen

  1. def _command_execute(command_s):
  2. """执行命令,例如command_s=['ls', '-al']"""
  3. sp = Popen(command_s, stdout=PIPE, stderr=STDOUT, close_fds=True)
  4. output_content = ''
  5. for line in iter(sp.stdout.readline, b''):
  6. output_content += line
  7. if len(output_content) > 1024 * 4:
  8. break
  9. sp.wait()
  10. return int(sp.returncode), output_content