Python 3 - TypeError: 'map' object is not subscriptable
python
Solution
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]
Problem
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() ```