In IDLE 3 python shell, how to use 'ls' command properly to get proper result

StackOverflow https://stackoverflow.com/questions/21153877

  •  28-09-2022
  •  | 
  •  

سؤال

I am using the following command

print Popen(['ls'], stdout=PIPE).communicate()[0]

which outputs

b'pi\ntem\ntemp\n'

P.S. I am using raspberry pi which has Raspbian Linux

هل كانت مفيدة؟

المحلول

You're using Python 3, which differentiates between strings and byte arrays. You need to decode the byte array returned by the communicate() method:

print(Popen(['ls'], stdout=PIPE).communicate()[0].decode('utf-8'))

Some useful information can be found here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top