looping through a file

Adam efkw at fawekm.com
Fri Jul 25 16:29:15 EDT 2003


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





More information about the Python-list mailing list