[Tutor] This Deletes All my Files

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Fri Feb 4 09:07:26 CET 2005


Ok, so in Python, arguments are evaluated first, left to right !
The outer-most function used in your sample is :
file.write(self, filename, mode)

So the first argument evaluated is "self" ... and in your case "self" is 
"open(item, 'w')" so the first thing your line does is opening for 
writing the file named by "item" and as "w" empty the file ... you can 
read it afterward, it will be empty !

Pierre

PS: general in programs: do NEVER rely on arguments evaluation order, 
unless you have no other choices and you are damn sure of what you're 
doing. In any case, do NEVER do that if you're no expert in the 
programming language used.

Chad Crabtree a écrit :
> I've tried this and I cannot figure out why this does not work.  I 
> figure this has something to do with order of operations.  I figured 
> someone would know exactly why it doesn't work.  Wouldn't this start 
> inside parens then from left to right?
> 
> open(item,'w').write(open(item,'r').read().replace(' ',''))
> 
> I tried this on the shell just trying to do some quick text
> replacement 
> because I could figure out how to get awk to do it, and I didn't want
> to 
> spend 5 hours RTFM.  I got it to work by breaking it up to several 
> statements, but I would like to know.
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Tutor mailing list