Opening textfiles in Python 3.2

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Larson, Moderators General, Prelates

Opening textfiles in Python 3.2

Postby dumbzebra » Sun Jul 24, 2011 1:31 pm UTC

Hi, python- and programming beginner here.
As part of the MIT OpenCoursework lectures, as an asignment, I was given the task to write my code based on this
(http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/ps5.py) predefined code that was given (my problem relates to the very first function "load_words()".
Without changing it, running that file should result in:
Code: Select all
Loading word list from file...
       83667 words loaded.
play_game not implemented.
play_hand not implemented.

Unfortunately, it shows:
Code: Select all
Traceback (most recent call last):
  File "C:\Python32\MIT\ps5.py", line 235, in <module>
    word_list = load_words()
  File "C:\Python32\MIT\ps5.py", line 27, in load_words
    inFile = open(WORDLIST_FILENAME,"r", 0)
ValueError: can't have unbuffered text I/O

Im working with python 3.2 (windows), the predefined code was written in python 2.6 or something.
What do I have to change to make it work?


edit: fixed links,spelling
As the great philosopher Socrates once said: "No."
User avatar
dumbzebra
 
Posts: 277
Joined: Thu Dec 10, 2009 4:59 pm UTC
Location: Somewhere on the moon.

Re: Opening textfiles in Python 3.2

Postby Zabaron » Sun Jul 24, 2011 5:14 pm UTC

If the code was written for Python 2, then you should use Python 2. Python 3 made a lot of library changes that are not backwards-compatible with Python 2. There is a tool included with Python 3 (2to3 I believe it's called), that will try to translate the Python 2 library calls to Python 3 ones, but I've never used it and I'm not sure how good it is.

-Zabaron
I love deadlines. I love that wooshing sound they make as they fly by. -Douglas Adams
User avatar
Zabaron
 
Posts: 108
Joined: Wed Jun 04, 2008 3:33 am UTC
Location: Georgia

Re: Opening textfiles in Python 3.2

Postby Cleverbeans » Thu Jul 28, 2011 4:14 pm UTC

Alternatively you can read the lines given by the debugger, look up the error in the docs and try to fix it. You are self-studying after all. :)
"Labor is prior to, and independent of, capital. Capital is only the fruit of labor, and could never have existed if labor had not first existed. Labor is the superior of capital, and deserves much the higher consideration." - Abraham Lincoln
Cleverbeans
 
Posts: 878
Joined: Wed Mar 26, 2008 1:16 pm UTC

Re: Opening textfiles in Python 3.2

Postby dumbzebra » Sat Jul 30, 2011 11:10 pm UTC

The problem with self-studying is, that i have no TA or prof to talk to. So i have to look up the error in google, and if that doesn't work, I'm pretty much screwed apart from asking in a forum.
Anyway, I did, however reluctantly, download the proper versin, and now I ask myself why they ever introduced parenthesis after print expressions.
Only seem to be redundant and make the scrip look more confusing.
As the great philosopher Socrates once said: "No."
User avatar
dumbzebra
 
Posts: 277
Joined: Thu Dec 10, 2009 4:59 pm UTC
Location: Somewhere on the moon.

Re: Opening textfiles in Python 3.2

Postby phlip » Sun Jul 31, 2011 1:31 pm UTC

dumbzebra wrote:and now I ask myself why they ever introduced parenthesis after print expressions.

So that print() can be just another function, rather than its own kind of special-cased statement. Which means you can do usual functional things with it, like passing it to map(). It also means one less special case in the language definition.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Opening textfiles in Python 3.2

Postby ntomlin » Wed Apr 11, 2012 4:47 am UTC

I too am going through the MIT course using python 3 for the extra practice. I just figured out how to make it work, so I thought I'd post it here. You have to change 2 lines:

python 2.x line:
Code: Select all
inFile = open(WORDLIST_FILENAME, 'r', 0)

new python 3.x line (get rid of 0 because unbuffered not allowed):
Code: Select all
inFile = open(WORDLIST_FILENAME, 'r')


python 2.x line:
Code: Select all
wordlist = split(line)

new python 3.x line:
Code: Select all
wordlist = line.split()
ntomlin
 
Posts: 1
Joined: Wed Apr 11, 2012 4:40 am UTC


Return to The Help Desk

Who is online

Users browsing this forum: No registered users and 4 guests