'While' question

Ben Keshet keshet1 at umbc.edu
Thu Aug 21 18:01:25 EDT 2008


Hi -

I am writing now my second script ever in python and need some help with 
'while'. I am reading text from a set of files and manipulating the data 
somehow. I use 'while 'word' not in line' to recognize words in the 
texts. Sometimes, the files are empty, so while doesn't find 'word' and 
runs forever. I have two questions:
1) how do I overcome this, and make the script skip the empty files? 
(should I use another command?)
2) how do I interrupt the code without closing Python? (I have ActivePython)

I do know that the strings I am searching for are within the first say 
50 lines.

Thanks!

Code:
|while 'PRIMARY' not in line:
                line = f.readline()[:-1]
            # copy scores
            while 'es' not in line:
                line = f.readline()[:-1]
                out_file.write(line)
                out_file.write(' ')
            print
            out_file.write('\n')
            f.close()
            out_file.close()

For example, 'PRIMARY' and 'es' do not exist when the file I am reading 
(f) is empty.
|





More information about the Python-list mailing list