Reputation: 443
I have this error when running this code with Python:
TypeError: "NoneType" object is unsubscriptable".
Code:
number = 0
with open('playlist.txt') as read_number_lines:
for line in read_number_lines:
if line.strip():
number += 1
number = number - 1
print 'number: ', number
for i in range(number):
author_ = raw_input('author: ')
line = input('line: ')
file = open('playlist.txt','a').writelines(' - ' + author_)[line]
How do I fix it?
Upvotes: 1
Views: 5205
Reputation: 336408
You've got a few problems in
file = open('playlist.txt','a').writelines(' - ' + author_)[line]
The immediate source of your error is that .writelines()
doesn't return anything (so it returns None
) which you're trying to index using [line]
. That produces your error.
Also, you shouldn't be calling that method on the open()
call directly.
The entire second for
loop is mysterious to me. You're opening that file again during each iteration of the loop (which you don't want to do; it probably wouldn't even work).
Perhaps you wanted to do something like
with open('playlist.txt','a') as file:
for i in range(number):
author_ = raw_input('author: ')
line = raw_input('line: ')
file.write(author + " - " + line)
but it's still hard to see the point of this...
Upvotes: 1