[Tutor] Help me abt this error

Luke Paireepinart rabidpoobear at gmail.com
Wed Oct 18 03:08:48 CEST 2006


Asrarahmed Kadri wrote:
>
> I changed to append mode and it is working.
> But the thing is that I have another problem.
> I am trying to insert usernames that are unique. Please look at the 
> code and help me.
>
> import sys
>
> fd = open('usr.txt','a')
>
append mode is write-only.
more comments further down.
>
>
> def chk(str):
>     global fd
>     fd.seek(0,0)
>     print fd
>     done = 0
>     list1 = []
>     answer = True
>    
>     while not done:
>        
>         aline = fd.readline ()
>        
>         if aline == "":
>             done = 1
>         else:
>             list1.append(aline)
>     print list1
>     for i in list1:
>         print i
>         j = i.split(':')
>         if str == j[0]:
>             answer = False
>             break
>     return answer
>
Can I politely request that you don't add color to your functions?
It makes it harder for me to read them.
I know you're trying to place emphasis on which functions are giving you 
problems, but if you were to just comment them
instead I'd be happy :)

For your problem about reading the file, I would suggest that you keep 
the contents in memory, or read the file each time and close it, instead 
of trying to keep an open copy of it around and seeking and such.  Since 
you're opening the file in append mode, you aren't able to do reads.
you could use 'a+', but I don't recommend it.
You'll probably run into weird behavior, and maybe only on certain 
inputs (I.E. scary bugs that you might not notice till your program is 
in use already.)

HTH,
-Luke


More information about the Tutor mailing list