Re: looping through a file

Discussion in 'Python' started by Bruno Desthuilliers, Jul 25, 2003.

  1. Adam wrote:
    > Psybar Phreak wrote:
    >
    >> hi all - ive just started with python (after having done java and c)
    >> and am
    >> having a little bit of trouble with the script. Im using as a login
    >> script.
    >>
    >> There's a users.dat file in the format
    >> user1
    >> user1password
    >> user2
    >> user2password
    >> ... etc
    >>
    >> at the moment - the code i work, only checks the first and second
    >> lines (ie.
    >> details of user1). but it appears that python doesn't have a do...
    >> while...
    >> until loop.
    >>


    No, but there are better ways to do it in Python :

    try:
    thefile = file(filename)
    except IOError:
    doWhatEverWithException()

    for line in thefile.readlines():
    doWhatEverWithTheLine()
    Bruno Desthuilliers, Jul 25, 2003
    #1
    1. Advertising

  2. Bruno Desthuilliers

    Adam Guest

    Psybar Phreak wrote:
    > hi all - ive just started with python (after having done java and c) and am
    > having a little bit of trouble with the script. Im using as a login script.
    >
    > There's a users.dat file in the format
    > user1
    > user1password
    > user2
    > user2password
    > ... etc
    >
    > at the moment - the code i work, only checks the first and second lines (ie.
    > details of user1). but it appears that python doesn't have a do... while...
    > until loop.
    >
    >
    > can anyone help?
    >
    > thanks!!
    >
    > Ive attached the appropriate part of the code i have working
    >
    > -------------------------------
    >
    > #!/usr/local/bin/python
    >
    > import cgi, string
    >
    > form = cgi.FieldStorage()
    >
    > if form.has_key('usernameField') and form.has_key('passwordField'):
    > users_username = string.strip(form['usernameField'].value)
    > users_password = string.strip(form['passwordField'].value)
    >
    > InFile = open('users.dat', 'r')
    >
    > file_username = string.strip(InFile.readline())
    > file_password = string.strip(InFile.readline())
    >
    >
    >
    >
    > authorised = 0
    >
    > while file_username and file_password:
    > if file_username == users_username:
    > if file_password == users_password: # correct
    > authorised = 1
    > break
    > else: # login matched but not
    > password
    > file_username = InFile.readline()
    > file_password = InFile.readline()
    > else: # neither match
    > file_username = InFile.readline()
    > file_password = InFile.readline()
    >
    >

    use the readline method. take advantage of the fact that readline
    returns an empty string when it reaches EOF, and do:
    while(line):
    line=readline
    Adam, Jul 25, 2003
    #2
    1. Advertising

  3. On Fri, 2003-07-25 at 22:24, Bruno Desthuilliers wrote:
    > for line in thefile.readlines():
    > doWhatEverWithTheLine()


    Or, if you're on a sufficiently new Python:

    for line in thefile:
    doWhateverWithTheLine()
    Heiko Wundram, Jul 26, 2003
    #3
  4. Quoth John J. Lee:
    > (Bengt Richter) writes:

    [woes with file iterators reading ahead]
    > > >I gathered from a recent thread that this has changed -- the file is
    > > >its own iterator now. Was the old behaviour ever released in 2.2, or
    > > >was it just part of a 2.3 beta?
    > > >

    > > Curious what was changed ...

    >
    > I'm not certain it has changed -- haven't tested.


    2.3 has the change, 2.2.2 does not:

    Python 2.2.2 (#1, Jun 19 2003, 11:01:48)
    >>> fp = file('foo')
    >>> iter(fp) is fp

    0

    Python 2.3b1 (#1, Apr 30 2003, 22:25:58)
    >>> fp = file('foo')
    >>> iter(fp) is fp

    True

    (I don't know about 2.2.3.)

    --
    Steven Taschuk
    "Please don't damage the horticulturalist."
    -- _Little Shop of Horrors_ (1960)
    Steven Taschuk, Jul 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rosario Morgan
    Replies:
    3
    Views:
    339
    bruno at modulix
    Jun 14, 2006
  2. planetmatt

    Looping through File Question

    planetmatt, Sep 5, 2007, in forum: Python
    Replies:
    7
    Views:
    428
    =?ISO-8859-1?Q?Ricardo_Ar=E1oz?=
    Sep 6, 2007
  3. AJ Hartley

    Looping through a log file

    AJ Hartley, Mar 4, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    80
    Eric Bohlman
    Mar 5, 2005
  4. Aaron
    Replies:
    2
    Views:
    507
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    261
Loading...

Share This Page