[Tutor] How to write strings with new line character in a file
Asrarahmed Kadri
ajkadri at googlemail.com
Fri Oct 13 12:23:19 CEST 2006
Here is the complete code:
fd is the file handle.
import sys
def check_dup(fd1):
print fd1
fd1.seek(0,0)
done = 0
list1 = []
while not done:
x = fd1.readline()
if x == "":
done = 1
else:
list1.append(x)
return list1
fname = raw_input("Enter the file name to write data to:\t")
fd = open(fname,'a+')
print fd
done = 0
while not done:
str = raw_input("Enter login name:\t to quit type 'q'")
if str == 'q':
done = 1
else:
flag = check_dup(fd)
print flag
if str in flag:
print "Login already exists.!!"
else:
fd.seek(0,2)
fd.write(str + '\n')
On 10/13/06, Bob Gailer <bgailer at alum.rpi.edu> wrote:
>
> Asrarahmed Kadri wrote:
> > Folks,
> >
> > I am trying to enter names in a file; each on a new line with this
> > code, but not working:
> >
> > done = 0
> > *while not done:
> > str = raw_input("Enter login name:\t to quit type 'q': ")
> > if str == 'q':
> > done = 1
> > else: *
> > * str = str + '\n'*
> > * fd.write(str)
> > *
> No can help without seeing the code that creates fd. Please post that.
> > The traceback is as under:
> >
> > Traceback (most recent call last):
> > File "scrap.py", line 38, in ?
> > fd.write(str)
> > IOError: (0, 'Error')
> >
> > Please help to rectify this error...
> > --
> > To HIM you shall return.
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
> --
> Bob Gailer
> 510-978-4454
>
>
--
To HIM you shall return.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061013/11ec78cb/attachment.htm
More information about the Tutor
mailing list