3 Replies - 3489 Views - Last Post: 13 September 2011 - 05:12 PM Rate Topic: -----

#1 azhelle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-June 11

Expected String Or Buffer Error

Posted 13 September 2011 - 12:00 AM


def func1:
    myfile = openthefile("hello.txt")

def openthefile(filename):

    f = open(str(filename), 'r')
    newdata = f.read
    f.close()

    return newdata




As shown above, I was trying to open a file in Python. What I don't understand is that even if I converted it to a string, I'm still having an error that says, "expected string or buffer". Is there something that I've done wrong here?

Will greatly appreciate your reply. Thanks

- Azhelle


Is This A Good Question/Topic? 0
  • +

Replies To: Expected String Or Buffer Error

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Expected String Or Buffer Error

Posted 13 September 2011 - 12:42 AM

Try:

def func1():#You've omitted '()',is this even correct? shouldn't it have '()' after 'func1'?
    myfile = openthefile("hello.txt")

def openthefile(filename):

    f = open(str(filename), 'r')
    newdata = f.read() #again, added '()' at the end of 'read'
    f.close()
    return newdata


This post has been edited by fromTheSprawl: 13 September 2011 - 12:43 AM

Was This Post Helpful? 1
  • +
  • -

#3 azhelle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-June 11

Re: Expected String Or Buffer Error

Posted 13 September 2011 - 06:21 AM

View PostfromTheSprawl, on 13 September 2011 - 12:42 AM, said:

Try:

def func1():#You've omitted '()',is this even correct? shouldn't it have '()' after 'func1'?
    myfile = openthefile("hello.txt")

def openthefile(filename):

    f = open(str(filename), 'r')
    newdata = f.read() #again, added '()' at the end of 'read'
    f.close()
    return newdata



oops, that was my bad...forgive me...but given the corrections, I'm still having the same error. I tried everything even adding single quote on the first and last part of the filename even though I'm not sure if that would make it a string...I'm really lost...
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Expected String Or Buffer Error

Posted 13 September 2011 - 05:12 PM

But it works dude. What version of Python are you using? I'm using 3+. Here I've tried two variations on your code:

1st variation:

>>> def func1():
...     myfile = openthefile('fruits.txt')
...     return myfile



>>> def openthefile(filename):
...     f = open(str(filename),'r')
...     newdata = f.read()
...     f.close()
...     return newdata
...



>>> fruits = func1()
>>> fruits
'Apple\nOrange\nCranberries'



2nd variation changes read() to readlines()


>>> def openthefile(filename):
...     f = open(str(filename),'r')
...     newdata = f.readlines()
...     f.close()
...     return newdata
...



>>> fruits = func1()
>>> fruits
['Apple\n', 'Orange\n', 'Cranberries']


And a 3rd variation to openthefile:

def openthefile(filename):
    with open(filename,'r') as f:
            newdata = f.read()
    return newdata



>>> fruits = func1()
>>> fruits
'Apple\nOrange\nCranberries'


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1