xsubprocess.py 548 B

123456789101112131415161718192021
  1. # update: 2022-4-25-18
  2. import subprocess
  3. def run_command(command, callback=False):
  4. """
  5. 执行shell命令
  6. """
  7. if callback:
  8. obj = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
  9. lines = obj.stdout.readlines()
  10. if len(lines) == 1:
  11. lines = str(lines[0], encoding='utf-8').strip('\n')
  12. return lines
  13. else:
  14. return subprocess.Popen(command, shell=True)
  15. if __name__ == '__main__':
  16. out = run_command('/home/server/resources/vms-files', callback=True)
  17. print(out)