-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutar _command
More file actions
37 lines (32 loc) · 1.1 KB
/
Executar _command
File metadata and controls
37 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import subprocess
import os
# Method 1: Using subprocess.run()
def run_command(command):
result = subprocess.run(command, shell=True, capture_output=True, text=True)
print("Output:\n", result.stdout)
print("Errors:\n", result.stderr)
# Method 2: Using subprocess.call()
def call_command(command):
result = subprocess.call(command, shell=True)
print("Return code:", result)
# Method 3: Using subprocess.Popen()
def popen_command(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
print("Output:\n", stdout)
print("Errors:\n", stderr)
# Method 4: Using os.system()
def os_system_command(command):
result = os.system(command)
print("Return code:", result)
# Example usage
if __name__ == "__main__":
cmd = "echo Hello, World!"
print("Using subprocess.run():")
run_command(cmd)
print("\nUsing subprocess.call():")
call_command(cmd)
print("\nUsing subprocess.Popen():")
popen_command(cmd)
print("\nUsing os.system():")
os_system_command(cmd)