ython provides several ways to execute programs or call system commands, some of which are:
- Using the
os
module: You can use theos
module to execute a program or call a system command. Here’s an example:
import os
os.system(“ls”)
This will execute the ls
command in the terminal.
- Using the
subprocess
module: Thesubprocess
module provides a more powerful way to execute programs or call system commands. Here’s an example:
import subprocess
result = subprocess.run([“ls”, “-l”], stdout=subprocess.PIPE)
print(result.stdout.decode())
This will execute the ls -l
command and capture its output to a variable called result
. Then it will print the output to the console.
- Using the
os.system()
method: You can use theos.system()
method to execute a system command. Here’s an example:
import os
os.system(“echo Hello World!”)
This will execute the echo Hello World!
command in the terminal.
- Using the
os.popen()
method: You can use theos.popen()
method to execute a system command and capture its output. Here’s an example:
import os
output = os.popen(“ls”).read()
print(output)
This will execute the ls
command and capture its output to a variable called output
. Then it will print the output to the console.
- sunny asked 1 month ago
- You must login to post comments
Please login first to submit.