KingJohnno
KingJohnno

Reputation: 602

Opening file error: TypeError: coercing to Unicode: need string or buffer, list found

I have the following code: Basically what I am doing, is looking for a .csv file, which could be in one of two places (or more depending) I have the two specific locations in a text file (LocationsFile.txt).

Out of this I want only to get specific fields for the student: This is the SSID Field

I have the following code, but the error it seems to give me is as follows:

    Tape Name130322
['\\\\....HIDDEN FOR Confidenciality .....StudentDB1_av.csv']
Success:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\Administrator\Desktop\John\Spreadsheet\Gui.py", line 291, in run
    findStudentData('130322','StudentDB1')
  File "C:\Users\Administrator\Desktop\John\Spreadsheet\Gui.py", line 62, in findStudentData
    with open(items) as f:
TypeError: coercing to Unicode: need string or buffer, list found

Code that is being executed is as follows: - please do be considerate when replying, as I am a 'newbie' python programmer!

def findStudentData(student_name,course):
student_name= student_name.lower()
configfiles = []

print "student_name" + student_name
for path in MMinfo_paths:
    configfiles.append(glob.glob(path+"/" + course+"*"))

for items in configfiles:
    print items
    print "Success:"
    heading = True

    with open(items) as f:
        content = f.read()

        if heading == True
            ssidCol = content.index('ssid')
            flagsCol = content.index('flags')
            nameCol = content.index('name')
            heading = False
            continue



        for item in rowData:
            if rowData.index(item) == nameCol:
                print rowData
            else:
                continue

Many thanks :-)

Upvotes: 1

Views: 9866

Answers (2)

Chris Barker
Chris Barker

Reputation: 2399

Right now, your configfiles looks like this:

[[file1, file2], [file3, file4]]

You can either do this:

for items in configfiles:
    for filename in items:
        with open(items) as f: ...

Or, you can replace configfiles.append with configfiles.extend. Append adds the list returned by glob as an element of the configfiles list, whereas extend adds each element of the list returned by glob to the configfiles list. Then, configfiles would be:

[file1, file2, file3, file4]

And you can just write:

for items in configfiles:
    with open(items) as f: ...

Upvotes: 2

DhruvPathak
DhruvPathak

Reputation: 43245

items is a list on your code, it should be a string instead.

The glob.glob function returns a list, hence your configfiles variables is a list of lists.

for items in configfiles:
    print items        # items is a list here
    print "Success:"
    heading = True

    with open(items) as f:  # list instead of string here, is incorrect.

Upvotes: 1

Related Questions