Strange thing: file.close did not show error but work wrong
Emile van Sebille
emile at fenx.com
Wed Nov 21 21:39:12 EST 2001
"Emile van Sebille" <emile at fenx.com> wrote in message
news:9tho04$2or14$1 at ID-11957.news.dfncis.de...
>
> "wqhdebian" <wqhdebian at 263.net> wrote in message
> news:db288b61.0111211822.2e903177 at posting.google.com...
> > f='d:\\d.txt'
> > fdw='d:\\matlabdata.txt'
> > import string
> > fd=open(f,'r')
> > fdw=open(fdw,'w+')
> > b=fd.readlines()
> > num=len(b)
> > print 'num='
> > print num
> > #print b
> > print type(b)
> > #a=string.split(b,' ')
> > print len(b)
> > #for i in b[:]:
> > # print i
> > fdw.write('i')
> > fd.close###########################
> > fdw.close######################### When I use like this ,there is no
> > warning and
> > ##############any error message,but the file do not have been
> > writen.After I change back to fdw.close(),Then it work well.
> >
>
>
> Yes, this is all as it should be.
>
> Python simply recognizes the name without error. In console mode the same
> line would have responded as though you had typed in print fdw.close which
> would then respond with something like "method close of file object".
>
Sorry, didn't catch the comment on the file not being written. I suspect
there you're running into an os specific issue. It did write on my win2k
box, and the expectation is that you shouldn't have to close() a file before
exiting, but I have heard people suggest that you flush() and close() to
ensure that the data is written.
Emile
More information about the Python-list
mailing list