[Tutor] how to read a text file, and find items in it

adam adam at lumanation.com
Thu Mar 9 01:46:51 CET 2006


hey guys!

I am trying to learn a bit of python, and thought it would be a challenge to make a script that reads a text file and returns all the instances of a word I specify. Basically a find and then a list of all the finds.

I am using a text file with some random words in it to test it out on

Thanks!

Adam



My script so far is this:

---------------------------------

import fileinput

print
print "Welcome to LIST YOUR FILEINS script"

print
print "STEP 01"

print "drag'n'drop your script from the Finder here"
script = raw_input(">")

print
print "The location of your script is"
print script

print

file=open(script)
text=file.readlines()
f = 'file'
for f in text: print f

----------------------------------------

The test file:

random
words
file in
filein
mac


More information about the Tutor mailing list