Chad Crabtree <flaxeater <at> yahoo.com> writes: > 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(' ',''))
Well, what your code says is this: 1. open item for writing and return a file object 2. call on that file object the write method (at this point its contents are wiped out) 3. open that same file for reading (but it's empty now) 4. read everything from it (nothing) 5. write nothing back to the file. You can test it by implementing a dummy open method and a dummy file class which log what happens to them: >>> s = "d:/tests/test.txt" >>> class dummyfile(object): ... def open(self, *args): ... print "dummyfile.open:", args ... def write(self, *args): ... print "dummyfile.write:", args ... def read(self, *args): ... print "dummyfile.read:", args ... return "" >>> def dummyopen(filename, type): ... print "dummyopen:", filename, type ... d = dummyfile() ... d.open(filename, type) ... return d >>> dummyopen(s, 'w').write(dummyopen(s, 'r').read()) dummyopen: d:/tests/test.txt w dummyfile.open: ('d:/tests/test.txt', 'w') <--- first open for writing dummyopen: d:/tests/test.txt r dummyfile.open: ('d:/tests/test.txt', 'r') <--- then for reading dummyfile.read: () dummyfile.write: ('',) > spend 5 hours RTFM. I got it to work by breaking it up to several > statements, but I would like to know. And that's the way you *should* write it - code like this doesn't deserve to work anyway :). Yours, Andrei _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor