Python Forum
subprocess.Popen() and encodings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess.Popen() and encodings
#1
I need to call 3rd party command-line tool from Python and communicate with it: pass commands and read their results. Tool started with the subprocess.Popen() and then I write to stdin and read from stdout. Here is simplified code
import subprocess

command = ['/path/to/executable', 'arg1', 'arg2', 'arg3']

instance = subprocess.Popen(command,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.DEVNULL,
                            universal_newlines=True)

# pass data and command to the tool
instance.stdin.write('/path/to/data\n-command\n')
instance.stdin.flush()

# get results
instance.stdout.readlines()
This works fine on Linux and Mac, but when I try to run it inside Windows command-line (cmd.exe) with file name containing non-ASCII characters, I get

Quote:UnicodeEncodeError: 'charmap' codec can't encode characters in position 53-59: character maps to <undefined>

As I understand this is because universal_newlines=True activates text mode and all input and output is decoded into some encoding (UTF-8 by default) while another encoding is used (e.g. CP1251 or other depending on the system locale). I tried to pass encoding='cp1251' but this does not help, filename turned into question marks and file can not be by commandline tool.

If I remove universal_newlines=True and pass all parameters as bytes objects encoded using UTF-8 encoding, it still does not work. Non-ASCII filenames encoded using \xNN escape sequences and can not be found by commandline tool.

I'm a bit lost here and probably missing something obvious. Is there a cross-paltform way to pass and read non-ASCII strings using subprocess.Popen() which will work for any locale?

I'm using Python 3.7.0 and Python 3.8.0.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information subprocess.Popen() suddenly giving me grief? davecotter 3 607 Dec-13-2023, 10:49 PM
Last Post: davecotter
  Use subprocess.Popen and time.sleep chucky831 2 1,944 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,998 May-07-2021, 03:26 PM
Last Post: herwin
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,618 May-04-2021, 08:58 PM
Last Post: liudr
  disable subprocess.popen prompt echo paul18fr 1 2,004 Feb-04-2021, 02:50 AM
Last Post: Larz60+
  how to pass the interactive string to Popen subprocess maiya 1 1,878 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  How to get program output from subprocess.Popen? glestwid 1 2,366 Aug-19-2020, 05:44 AM
Last Post: buran
  ModuleNotFoundError: no module named 'encodings' grunge10111 1 3,817 May-29-2020, 02:22 AM
Last Post: Larz60+
  Character Encodings Evil_Patrick 1 2,587 Nov-17-2019, 02:23 PM
Last Post: snippsat
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 5,916 Nov-08-2019, 05:00 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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