looping through a file

Ilariu Raducan lale at fotonation.com
Wed Apr 30 06:57:48 EDT 2003


 From Python Doc:
readline([size])
Read one entire line from the file. A trailing newline character is kept 
in the string2.11 (but may be absent when a file ends with an incomplete 
line). If the size argument is present and non-negative, it is a maximum 
byte count (including the trailing newline) and an incomplete line may 
be returned. An empty string is returned when EOF is hit immediately. 
Note: Unlike stdio's fgets(), the returned string contains null 
characters ('\0') if they occurred in the input.


If your file has no empty lines (not even at  the end) this shoul work
users= {}
username =InFile.readline()
while(len(line)!=0):
	password=InFile.readline()
	users[username]=password
	username=InFile.readline()

Regards,
Lale
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()
> 
> 





More information about the Python-list mailing list