3

i'm currently running this as a piece of my code for an i/o stream - i'm getting the following error TypeError: 'map' object is not subscriptable for the print (bytest[:10]). What's the proper way run it in Python 3?

with open("/bin/ls", "rb") as fin: #rb as text file and location 
  buf = fin.read()
bytes = map(ord, buf)    
print (bytes[:10])
saveFile = open('exampleFile.txt', 'w')
saveFile.write(bytes)
saveFile.close()
user6096418
  • 101
  • 1
  • 1
  • 6

1 Answers1

6

In Python 3 map returns a generator. Try creating a list from it first.

with open("/bin/ls", "rb") as fin: #rb as text file and location 
  buf = fin.read()
bytes = list(map(ord, buf))
print (bytes[:10])
saveFile = open('exampleFile.txt', 'w')
saveFile.write(bytes)
saveFile.close()

If you think that is ugly, you can replace it with a list generator:

bytes = [ord(b) for f in buf]
jdehesa
  • 58,456
  • 7
  • 77
  • 121