Python Forum
Encoding issue while running Sysinternals Autorunsc via Python subprocess
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Encoding issue while running Sysinternals Autorunsc via Python subprocess
#1
I have a Python 3.6.0 script where I run autorunsc v13.71 (https://technet.microsoft.com/en-us/sysi...63902.aspx) on the system (x86 or x86_64 version, according to the system bitness using platform.machine()). If I run autorunsc directly from the terminal (CMD or Powershell) I get the output as expected, no issues (snip from the output):

[Image: KlN1f.png]

But, if I try to run it using my code I get this messy output:

[Image: YcKkv.png]

I'm using Window's default Notepad to open the output text file. People should be able to read it using Notepad, they won't be able to download a code reader like Notepad++, ST3, etc.

----------

My code (removed some parts to keep it short and direct):

    
   #!/usr/bin/env python
   # -*- coding: utf-8 -*-
   
   import platform
   import socket
   import subprocess
   from time import gmtime, strftime
   from pathlib import Path
   
   HOSTNAME = socket.gethostname()
   SYS_ARCH = platform.machine()  # AMD64 or x86
   ROOT_PATH = Path(__file__).parent
   
   
   def get_current_datetime():
    return strftime("%Y-%m-%d %H:%M:%S UTC%z", gmtime())
   
   
   def run_command(output_file, command_name, command_args, system_cmd=False):
    output_file.write(f'---------- START [{command_name} {SYS_ARCH}] {get_current_datetime()} ----------\n')
    output_file.flush()
   
    file_path = command_name if system_cmd else str(ROOT_PATH / 'tools' / SYS_ARCH / (command_name + '.exe'))
    subprocess.call([file_path] + command_args, stdout=output_file, shell=True, universal_newlines=True)
   
    output_file.write(f'---------- ENDED [{command_name} {SYS_ARCH}] {get_current_datetime()} ----------\n\n')
    output_file.flush()
    print(f'[*][{command_name} {SYS_ARCH}] done')
    
    
    def main():
     output_file = ROOT_PATH.parent / (HOSTNAME + '.txt')
     with open(output_file, 'w', encoding='utf-8') as fout:
     run_command(output_file=fout, command_name='autorunsc', command_args=['-h', '-nobanner', '-accepteula'])
     
    
    if __name__ == '__main__':
     main()
   
----------

File structure:

- folder\
 - app.py (the code shown here)
 - tools\
    - AMD64\
        - autorunsc.exe
    - x86\
        - autorunsc.exe

----------

I believe it's something to do with the output of autorunsc, I read somewhere it returns the output encoded as UTF-16. The thing is that I run many other Sysinternals EXEs and append the output to the same file (using my run_command function), and all of them work flawlessly, but this one. How can I get this right?

Found the solution:

 # IF-ELSE to handle the 'autorunsc' output, which is UTF16
    if command_name == 'autorunsc':
    	result = subprocess.Popen([file_path] + command_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    	text = result.stdout.read().decode('UTF16')
    	for line in text:
    		output_file.write(line)
    else:
    	subprocess.call([file_path] + command_args, stdout=output_file, stderr=output_file)	
Keep it simple, stupid — kiss principle.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running script with subprocess in another directory paul18fr 1 3,483 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Encoding Issue in eyed3 giddyhead 2 1,790 Jul-22-2022, 02:03 AM
Last Post: giddyhead
  Error when running mktorrent subprocess command pythonnewbie138 4 3,770 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  python loop in subprocess vinothkumargomu 6 3,113 Jul-06-2020, 12:02 PM
Last Post: vinothkumargomu
  Python 36-32 problem encoding module not found mkgrtn 0 2,384 Apr-14-2020, 09:57 PM
Last Post: mkgrtn
  how to set focus when running a script using subprocess Malt 0 3,075 Sep-26-2019, 08:07 AM
Last Post: Malt
  Issue with text encoding ans smtplib.SMTP.sendmail() JustSomeUsername383 1 4,111 Jul-23-2019, 03:15 PM
Last Post: JustSomeUsername383
  Python Error in subprocess.py roydianton90 1 5,547 Dec-14-2018, 11:26 AM
Last Post: jeanMichelBain
  OS command via python subprocess module alinaveed786 21 10,635 Oct-23-2018, 05:40 AM
Last Post: alinaveed786
  subprocess in thread python to check reachability anna 3 3,387 Sep-05-2018, 04:01 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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