How to run shell command, capture the output, then write it into textfile?

Joined
Dec 19, 2024
Messages
7
Reaction score
0
How to run shell command, capture the output, then write it into textfile?

I tried to use code below:
Code:
from subprocess import run

data = run("cstool arm64 1F2003D5", capture_output=True, shell=True, text=True)
print(data.stdout)

hfile3 = open("result1.txt", "a")
hfile3.write(data.stdout())
hfile3.close()

It can successfully print what I want, but it cannot write into text file

there is error message: 'str' object is not callable

How can I capture / assign the stdout result into a variable as string?

Can I do something like:

Result = data.stdout()?

Thank You
 
Joined
Jul 4, 2023
Messages
527
Reaction score
70
You can also do it from the Shell Command level
Python:
from subprocess import run

# Run the command and redirect output to the file
data = run("cstool arm64 1F2003D5 > result1.txt", capture_output=True, shell=True, text=True)

# Check if the command was successful (returncode 0 indicates success)
if data.returncode == 0:
    # Open the file and print its contents
    with open("result1.txt", "r") as hfile3:
        print(hfile3.read())
else:
    print("The command failed to execute.")
or
Python:
from subprocess import run

# Define the result file name
result_file_name = "result1.txt"

# Run the command and redirect output to the file
data = run(f"cstool arm64 1F2003D5 > {result_file_name}", capture_output=True, shell=True, text=True)

# Check if the command was successful (returncode 0 indicates success)
if data.returncode == 0:
    # Open the file and print its contents
    with open(result_file_name, "r") as hfile3:
        print(hfile3.read())
else:
    print("The command failed to execute.")


[ Redirection Operators (>, >>) in Windows and MS-DOS ]
 
Last edited:
Joined
Dec 19, 2024
Messages
7
Reaction score
0
You can also do it from the Shell Command level
Python:
from subprocess import run

# Run the command and redirect output to the file
data = run("cstool arm64 1F2003D5 > result1.txt", capture_output=True, shell=True, text=True)

# Check if the command was successful (returncode 0 indicates success)
if data.returncode == 0:
    # Open the file and print its contents
    with open("result1.txt", "r") as hfile3:
        print(hfile3.read())
else:
    print("The command failed to execute.")
or
Python:
from subprocess import run

# Define the result file name
result_file_name = "result1.txt"

# Run the command and redirect output to the file
data = run(f"cstool arm64 1F2003D5 > {result_file_name}", capture_output=True, shell=True, text=True)

# Check if the command was successful (returncode 0 indicates success)
if data.returncode == 0:
    # Open the file and print its contents
    with open(result_file_name, "r") as hfile3:
        print(hfile3.read())
else:
    print("The command failed to execute.")


[ Redirection Operators (>, >>) in Windows and MS-DOS ]

Thank You

I'll try it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,073
Messages
2,570,538
Members
47,195
Latest member
RedaMahuri

Latest Threads

Top