[Tutor] file read - rewind?

Nick Lunt nick at javacat.f2s.com
Mon Jul 26 18:27:59 CEST 2004


Hi Vicki,

I dont use the wx toolkit, but to rewind a file you want to use seek()

Eg

>>> f = open('/nvmixer.log')
>>> for line in f:
... 	print line,
...
[InstallShield Silent]
Version=v6.00.000
File=Log File
[ResponseResult]
ResultCode=0
[Application]
Name=NvMixer
Version=1.50.000
Company=NVIDIA Corporation
Lang=0009
>>> f.tell()
176L
>>> f.seek(0)
>>> f.tell()
0L

filename.seek(0) will rewind the file back to the beginning.
There's no easy way to rewind back to a certain line number that Im aware
of.

>filename.seek(0)
>filename.seek(10)

will now start reading from the 10th character, remembering that line chars
start at 0.

Hope that helps
Nick.


-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]On
Behalf Of vicki at thepenguin.org
Sent: 26 July 2004 17:07
To: tutor at python.org
Subject: [Tutor] file read - rewind?


I want to read in a file, act on each line of the file, and then start at
the beginning again. I don't see a rewind function in the fileinput
module, yet it still does not work by simply executing the for line in
inputfile again. What am I missing? The while loop should repeat until
STOP_READ is set to TRUE.

--Vicki

---------------------------------------------------------------------------
        STOP_READ = wx.FALSE
        first_iteration = wx.TRUE
        #While not STOP_READ, iterate through lines in file
        while STOP_READ == wx.FALSE or first_iteration == wx.TRUE:
            print "Got into while loop"
            for line in input.readlines():
                if len(line)>1:
                    first_iteration == wx.FALSE
                    tokens=line.split("|")

                    self.command = tokens[0][0]+tokens[0][1]
                    self.arguments =
tokens[0].lstrip(tokens[0][0]+tokens[0][1]+" ")
                    print self.command
                    print self.arguments
                    self.ProcessCommand(self.command, self.arguments)
                    wx.GetApp().Yield()

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list