files

Hi Stephan
Your problem is not a wxPython problem,
but a pure python question.
See Python Tutorial, section 7.2 Reading and Writing files

As far I understood, you cann't read a file correctly.
This is because, you do not check the end of the file.

Here a small and working small code.

···

#-----------------------------
# rwfile.py
#-----------------------------

def main():
    #write a text file of 3 lines
    f = open('name.txt', 'w')
    f.write('line 1\n')
    f.write('line 2\n')
    f.write('line 3\n')
    f.close()

    #variant 1
    #read the lines from the previous file
    #line by line
    f = open('name.txt', 'r')
    tmpline = f.readline()
    while len(tmpline) != 0: #test EOF
        print 'line:', tmpline
        tmpline = f.readline()
    f.close()

    #variant 2
    #read all the lines in single shoot
    #lines are in a list
    f = open('name.txt', 'r')
    alllines = f.readlines()
    f.close()
    for e in alllines:
        print e

#-----------------------------

if __name__ == '__main__':
    main()

#eof-----------------------------

You wrote:

Even better is that i can read the file as soon as the

lines are
written into the file..
Sound strange, why do you want to read just a written line?
Anyway, it is possilble to 'walk' in a opened file with the
seek command. See the tutorial.

Jean-Michel Fauth, Switzerland

Jean-Michel Fauth wrote:

Here a small and working small code.

It works, but here is a slightly more "pythonic" version...

#!/usr/bin/env python2.2
# the above line for the unix folks....

···

#-----------------------------
# rwfile.py
#-----------------------------

def main():
    #write a text file of 3 lines
    f = open('name.txt', 'w')
    f.write('line 1\n')
    f.write('line 2\n')
    f.write('line 3\n')
    f.close()

    #variant 1
    #read the lines from the previous file
    #line by line
    
    f = open('name.txt', 'r')
    while True: # this contsruction provides a wya to have the line read
in only one place.
        tmpline = f.readline()
        if not tmpline: # an empty string is False
            break
        print 'line:', tmpline, # note that the line retains the "\n" at
the end.
                                # The comma at the end of a print
statement
                                # suppresses the one that print usually
puts in.
    f.close()

    #variant 2
    #read all the lines in single shoot
    #lines are in a list
    f = open('name.txt', 'r')
    alllines = f.readlines()
    f.close()
    for e in alllines:
        print e,

    #variant 3 (I think this is only Python2.2 and above:
    f = open('name.txt', 'r')
    for e in f:
        print e,
    
#-----------------------------

if __name__ == '__main__':
    main()

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

thank you jean-michel and chris for your support and tips..
i will try it...step by step it seems to work and my small program is now
evolving...

stephan

Stephan Huijgen wrote:

thank you jean-michel and chris for your support and tips..

you're welcome. As it seems you are still learning the ins and outs of
Python, as well as wxPython, you might want to post questions to the
python tutor list:

http://www.python.org/psa/MailingLists.html#tutor

I understand that's pretty helpfull bunch

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov