Attempt to launch another .exe and save the console output to a text file

Get and save the console output of the process started by subprocess.

You can save the console output of test.exe to a text file with the following code. Both test.bat (and .bat file) can save console output to a text file.

** Use subprocess.Popen (.., stdout = .., stderr = ..), send console output to this process with stderr = subprocess.PIPE, and start withproc.communicate () Wait for the end of the process. (If there is no proc.communicate (), it will continue without waiting for the end, and it will be parallel processing) **

trial.py



# -*- coding: utf-8 -*-

#To get / save the console output of the process started by subprocess
# ref: https://www.it-swarm-ja.tech/ja/python/subprocesscall%EF%BC%88%EF%BC%89%E3%81%AE%E5%87%BA%E5%8A%9B%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B/968744582/

import subprocess

def main(): #For execution switching
    run__main1()

# sec:Get and save console output

def run__main1():
    #result:
    # test.exe console output can be saved
    # test.bat can also save console output,.bat is also possible
    
  # case: FAIL
    # subprocess.call("test.exe > res.log.txt")
    
  # case: Popen(.., stdout=..)use
    with open("res.log.txt", 'w') as file_log:
        proc = subprocess.Popen(
            ["test.bat"], 
            stdout=file_log, stderr=subprocess.PIPE)
        proc.communicate() #Required for standby, if output is required: stdout, stderr = proc.communicate()
    print("return code:", proc.returncode) # DEBUG:For standby / end confirmation
  # case: end
    
    print("ended.")

# sec: entry

if __name__ == "__main__": main()

reference

https://www.it-swarm-ja.tech/ja/python/subprocesscall%EF%BC%88%EF%BC%89%E3%81%AE%E5%87%BA%E5%8A%9B%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B/968744582/

Recommended Posts

Attempt to launch another .exe and save the console output to a text file
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
Output python log to both console and file
Save the object to a file with pickle
Output a binary dump in binary and revert to a binary file
Save the pystan model and results in a pickle file
Change the standard output destination to a file in Python
Write standard output to a file
How to overwrite the output to the console
Output the key list included in S3 Bucket to a file
Process Splunk execution results using Python and save to a file
How to output the output result of the Linux man command to a file
I want to output to the console coolly
How to split and save a DataFrame
How to get colored output to the console
Start the webcam to take a still image and save it locally
How to save the feature point information of an image in a file and use it for matching
How to count the number of elements in Django and output to a template
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
I want to make a music player and file music at the same time
Zip-compress any file with the [shell] command to create a file and delete the original file.
[Python] Concatenate a List containing numbers and write it to an output file.
Save an array of numpy to a wav file using the wave module
Python Memorandum: Refer to the text and edit the file name while copying the target file
[Python] You can save an object to a file by using the pickle module.
[Django] Test to send a file by POST and check the returned context [TDD]
I want to replace the variables in the python template file and mass-produce it in another file.
[Mac] A super-easy way to execute system commands in Python and output the results
Save the text of all Evernote notes to SQLite using Beautiful Soup and SQLAlchemy
Automatically determine and process the encoding of the text file
Created a module to monitor file and URL updates
Convert a text file with hexadecimal values to a binary file
How to turn a .py file into an .exe file
Output the output result of sklearn.metrics.classification_report as a CSV file
Create an audio file with the text-to-speech function with Google Text To Speak and check the text as a guide for the speech for 3 minutes.
[Sublime Text 2] Always execute a specific file in the project
[python] Change the image file name to a serial number
Temporarily save a Python object and reuse it in another Python
Build a Python environment and transfer data to the server
[Python] How to output a pandas table to an excel file
What to do if you cat or tail a binary file and the terminal is garbled
What to do if you grep a text file and it becomes Binary file (standard input) matches
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
I want to extract the tag information (title and artist) of a music file (flac, wav).
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.