Stack Exchange
What does TypeError: 'file' object has no attribute '__getitem__' mean?
I have this code:
     My_Directory = r"/Users/schuylerraeschroerborges/Desktop/Mars_Project/mcam_image_and_label/0984MR0043380090502893E01_DRCL"     # Get data from directory
     My_Extension = ".LBL"

     WorkingDirectory = "/Users/schuylerraeschroerborges/Desktop/Mars_Project/"  # Work in same directory

     f = open('mcam_image_and_label/0984MR0043380090502893E01_DRCL.LBL','r')    # Open label file
     lines = f.readlines() #index lines
     g = open('Expanded_data.csv','w+')   # Write in excel spreadsheet
     lines = g.readlines()


     ID_line = f[21]
     ID = re.split('"',f[21])[2]
     LO_line = f[134]
     LO = re.split('(',f[134])[2]
     DI_line = f[442]
     DI = re.split('=',f[442])[2]
     AZ_line = f[445]
     AZ = re.split('=',f[445])[2]
     VE_line = f[446]
     VE = re.split('=',f[446])[2]
     newline = ID + '\t' + LO + '\t' + DI + '\t' + AZ + '\t' + VE + '\n'
     g.writelines(newline)

I keep gettting this error, TypeError: 'file' object has no attribute '_getitem__', after I run the code in Python 2.7. What does this error mean? Do I need to change the name of a file or something?

You stored the lines in lines, not f; f is the file object. (And you’ll probably not want to call readlines() on the output file.)

You are trying to index object f which is a file like object returned by open() function.
Internally, indexing is implemented via __getitem__ special method which the file object doesn't implement, this is the reason you are seeing that error message.
The error is on the line where you are trying to assign ID_lines = f[21], perhaps you want to assign lines[21] instead.

I get this error when I change "f[21]" to "lines[21]": IndexError: list index out of range

@Mark: That's probably because you're overwriting the lines you read from f with an empty list of lines read from g (which is truncated when you open it with mode "w+").
