xsubprocess.py 800 B

123456789101112131415161718192021222324252627
  1. # update: 2024.9.4
  2. """
  3. cd /media/nvidia/nvme0n1/server/repositories/repositories/sri-project.demo-py/3rdparty/xlib
  4. python3 xsubprocess.py
  5. """
  6. import subprocess
  7. def run_command(command, callback=False):
  8. """
  9. 执行shell命令
  10. """
  11. if callback:
  12. obj = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
  13. lines = obj.stdout.readlines()
  14. if len(lines) == 1:
  15. lines = str(lines[0], encoding='utf-8').strip('\n')
  16. return lines
  17. else:
  18. return subprocess.Popen(command, shell=True)
  19. if __name__ == '__main__':
  20. command_line = f'find /media/nvidia/nvme0n1/ZJ_PRO_test -type f | wc -l'
  21. # command_line = 'du -sh /media/nvidia/nvme0n1/test | awk \'{print $1}\''
  22. out = run_command(command_line, callback=True)
  23. print(out)