2

I'm working on (French) PC with Python 3.4

for line in os.popen('dir'): print(line.rstrip())

I get the first line as expected

Le volume dans le lecteur C s'appelle SYSTEME

but for the second (with é)

Le numéro de série du volume est C250-47DD

I get the error message :

return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u201a' in position 7: character maps to
 <undefined>

What can I do ? In advance thanks for your help

CSJNL
  • 57
  • 1
  • 5
  • you should use subprocess *Deprecated since version 2.6: This function is obsolete.*, or just use os.listdir. – Padraic Cunningham Nov 21 '14 at 18:11
  • Possible duplicate of [os.popen().read() - charmap decoding error](https://stackoverflow.com/questions/42033334/os-popen-read-charmap-decoding-error) – user202729 Aug 09 '18 at 12:00

1 Answers1

3

You need to encode your line before printing by proper encoding :

for line in os.popen('dir'): 
      print(line.rstrip().encode('UTF-8')) # as utf8 is a universal encoding i use it you can use another too
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • [Doesn't seem to work...?](https://tio.run/##HcgxDoAgDADAWV5BWKCDxMSF@BY3hchg29Sa4OvRuF2OHz0I597rySRq6TKFxDZb8XNk4oxBPEtFLdatLU3Ow2KGf0KLcqlUDhAzbrTn4G8tY/IAvb8) or is this windows-only? (by the way there is a (newer...) duplicate to the question) – user202729 Aug 09 '18 at 11:59