Advertisement
harsh7i

CallBashCommand.py

Feb 22nd, 2023 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. """ Getting Value """
  2.  
  3. import subprocess
  4.  
  5. # Run the Bash command "ls -l" and capture the output
  6. output_bytes = subprocess.check_output(["ls", "-l"])
  7. output_str = output_bytes.decode("utf-8")
  8.  
  9. # Print the output
  10. print(output_str)
  11.  
  12. """ Just run Command """
  13.  
  14. import subprocess
  15. # Run the Bash command "ls -l" and capture the output
  16. output = subprocess.check_output(["ls", "-l"])
  17. # Print the output
  18. print(output)
  19. import subprocess
  20.  
  21.  
  22. import subprocess
  23.  
  24. # Execute the command
  25. result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  26.  
  27. # Check if the command ran successfully
  28. if result.returncode == 0:
  29.     print('Command ran successfully')
  30. else:
  31.     print('Command failed with return code:', result.returncode)
  32.     print('Error message:', result.stderr.decode('utf-8'))
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement