1

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?

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Black_Ram
  • 343
  • 5
  • 9
  • 19

1 Answers1

1

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...

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561