[Tutor] Mysterious syntax errors and file-reading issues

Jeremiah Hill jhill at maingauche.org
Fri Jul 14 22:00:51 CEST 2006


I'm trying to write a program that will read a text file and return a random
subset of the words contained therein.  (Its purpose is to generate lists of
words for spelling bees.  I'm a teacher.)  Here's what I have so far:

************ CODE STARTS *******************
L1 = []                # create an empty list
L2 = []                # and another one
import os
def exists('/Users/username/Documents/python/word_bank.txt'):
    return os.access('/Users/usernameDocuments/python/word_bank.txt',
os.F_OK)
for line in
open('/Users/username/Documents/python/word_bank.txt').readlines():
L1.append(line)    # empty the word bank into a list
for i in range(len(L1)):    # trim off the newlines
    L1[i] [:-1]
import random
random.shuffle(L1)        # shuffle the words
L1[0:3]

************ CODE ENDS **********************

Two problems have appeared:

1.  I get a "syntax error" message at the line that starts with "def
exists", but I don't see the mistake.

2.  If I take out that line, I get "errno 2" at the next line; the
interpreter seems convinced that that file doesn't exist, though it does.  I
actually tried putting os.path.exists() into the interpreter, and it
returned 'true', but it still doesn't see the file when I try to run this
program.

How can I solve or circumvent these problems?  I appreciate any help, even
hints, but frankly if anyone's willing to explain the solution outright I'd
be grateful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060714/9d910dd3/attachment.html 


More information about the Tutor mailing list