Page 1 of 1

File object is referenced as string

Posted: Fri May 09, 2014 5:43 pm
by arawra
Not sure why my fileObj variable is being referenced as a string.

TypeError: 'str' doesn't support buffer interface

Code: Select all

@SayCommand('!test')
def test(player, teamonly, CCommand):
    fileObj = open('test.txt','wb')
    for x in dir(PlayerEntity):
        fileObj.write(x) #error here
    fileObj.close()

Posted: Fri May 09, 2014 5:57 pm
by satoon101
You need to open in "w" mode not "wb". Also, use context management:

Syntax: Select all

@SayCommand('!test')
def test(player, teamonly, command):
with open('test.txt', 'w') as fileObj:
fileObj.write(x + '\n')


Also:
http://www.sourcepython.com/showthread.php?527

No more need to use "code" blocks when Python syntax blocks work.