How to open a file, read from it, and retrieve information from it?
Eric @ Zomething
eric at zomething.com
Tue Mar 9 23:25:29 EST 2004
bugsyboy9 at yahoo.com described:
> >From a GUI interface, a user needs to request a string to be searched
> from a file and bring the string back, if it is in the file, and
> display it in a different field within the same GUI. Basically, I
> need to be able to open a file, parse its contents, and bring the
> requested information back and display it in a GUI window.
The fun is in the doing, but here are some links you might want to explore:
File objects: http://www.python.org/doc/current/lib/bltin-file-objects.html
Tkinter GUI: http://www.pythonware.com/library/an-introduction-to-tkinter.htm
Regular expressions: http://www.python.org/doc/current/lib/module-re.html
here's how easy it can be (to do something easy) in Python:
IDLE 1.0
>>> from easygui import *
>>> fname=fileopenbox() #opens a file dialog and returns a selected file name
>>> fl=file(fname,'r') #opens the file for reading
>>> fString=fl.read() #reads the file into a string
>>> pos=fString.find("contract") #finds the first position of the string "contract" in the file string
>>> pos
273
If you start playing with it, you'll start enjoying it, I say.
Try, do, Google, repeat.
Eric Pederson
Pythonista since... February 2004!
More information about the Python-list
mailing list