Strange thing: file.close did not show error but work wrong 
Author Message
 Strange thing: file.close did not show error but work wrong

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.

    I use python2.1.1 under windows



Mon, 10 May 2004 10:22:34 GMT  
 Strange thing: file.close did not show error but work wrong


Quote:
> 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".

HTH,

--

Emile van Sebille

---------



Mon, 10 May 2004 10:25:24 GMT  
 Strange thing: file.close did not show error but work wrong



Quote:



> > 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



Mon, 10 May 2004 10:39:12 GMT  
 Strange thing: file.close did not show error but work wrong

Quote:


> > 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.

It's semantically consistent with the rest of Python, but it's
suboptimal from a human-factors perspective, IMHO; people who are used
to Perl or Delphi or Pascal will expect it to call fdw.close.  It
would be nice if the interpreter were smart enough to issue a warning
in this case: a method is gotten and then immediately discarded.


Wed, 12 May 2004 11:53:11 GMT  
 Strange thing: file.close did not show error but work wrong

Quote:



> > > 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.

> It's semantically consistent with the rest of Python, but it's
> suboptimal from a human-factors perspective, IMHO; people who are used
> to Perl or Delphi or Pascal will expect it to call fdw.close.  It
> would be nice if the interpreter were smart enough to issue a warning
> in this case: a method is gotten and then immediately discarded.

Unfortunately this would require special-casing the
"expression-as-statement" construct. This would probably gobble up far more
time than could be justified. How would you then have the statements treated
in interactive interpreter sessions, where normally the method would be
identified as the result of the expression?

regards
 Steve
--
http://www.holdenweb.com/



Fri, 14 May 2004 21:56:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Not doing things

2. Strange "file not open" error

3. Device file not closing on close command

4. Closed file not really closed

5. Playing a wav file while doing other things?

6. Calling Matlab from Labview returns a 1047 error - what am I doing wrong

7. HttpDist - getting errors, What I'm doing wrong

8. MF-COBOL and DLL's: Call shows strange errors

9. File Creation error - it worked in C4 but not in C5

10. Strange thing with ASCII files

11. Strangest thing happening with data files

12. Strange Things On File Server

 

 
Powered by phpBB® Forum Software