Python Forum
How to run shell command, capture the output, then write it into textfile?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to run shell command, capture the output, then write it into textfile?
#1
How to run shell command, capture the output, then write it into textfile?

I tried to use code below:
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
Reply
#2
I think I got the answer,
hfile3.write(data.stdout)
Thank You
Reply
#3
I've ever used something like the following snippet that writes in the txt file what the shell prints

import os, subprocess

Path = os.getcwd()  
txtFile = 'mytxt.txt' 
shellName = 'xxx.sh'
     
with open (Path + '/' + txtFile, 'a') as f:
    command_line = f"sh {shellName}"
    args = command_line.split()
    process = subprocess.run(args, stdout=f, text=True)
tatahuft likes this post
Reply
#4
(Dec-20-2024, 07:03 AM)paul18fr Wrote: I've ever used something like the following snippet that writes in the txt file what the shell prints

import os, subprocess

Path = os.getcwd()  
txtFile = 'mytxt.txt' 
shellName = 'xxx.sh'
     
with open (Path + '/' + txtFile, 'a') as f:
    command_line = f"sh {shellName}"
    args = command_line.split()
    process = subprocess.run(args, stdout=f, text=True)

Thank You

I'll try it
Reply
#5
another version

from subprocess import check_output
 
txt_file = 'shell_output.txt' 
shell_command = 'ls ./'
      
with open (txt_file, 'a') as f:
    shell_output = check_output(shell_command, shell=True)
    f.write(shell_output.decode())
Larz60+ likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  textfile to customtkinter combobox janeik 6 3,940 Aug-31-2023, 02:50 AM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 2,076 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 2,080 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to write a part of powershell command as a variable? ilknurg 2 1,860 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Open a dos shell for ssh command martinmistere 6 2,907 Jun-07-2022, 12:24 PM
Last Post: martinmistere
  Os command output in variable shows wrong value paulo79 2 2,293 Apr-09-2022, 03:48 PM
Last Post: ndc85430
Photo how I write the output into xml file in python? 3lnyn0 1 3,917 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  Command output to Variable ironclaw 1 3,431 Aug-26-2021, 06:55 PM
Last Post: bowlofred
  How to input & output parameters from command line argument shantanu97 1 3,553 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Sound Approach for Running a Shell Command? matt_the_hall 8 4,824 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020