Re: copying files into one
On Sunday 14 May 2006 05:09, Gary Wessle wrote: Hi I am looping through a directory and appending all the files in one huge file, the codes below should give the same end results but are not, I don't understand why the first code is not doing it. thanks Hi there - I think you might need to give a more whole code sample when asking this question, as it's all a bit ambiguous - for instance file is a type, like string or list but... combined = open(outputFile, 'wb') for name in flist: if os.path.isdir(file): continue ^If you can successfully get past this line, you must have reused file to describe a string (which is probably quite a *BAD* idea ;) ) and just not included some of the code... BUT infile = open(os.path.join(file), 'rb') This line suggests it's a list, so I don't know. Argh. Anyway, I'm not being pernickety, just pointing out that it's a little too ambiguous - the code sample you gave alone would not work at all.. # CODE 1 this does not work tx = infile.read(1000) if tx == : break combined.write(tx) infile.close() # CODE 2 but this works fine for line in infile: combined.write(line) infile.close() combined.close() Hope to help when you post back, Cheers, Ten -- There are 10 types of people in this world, those who understand binary, and those who don't. -- http://mail.python.org/mailman/listinfo/python-list
Re: copying files into one
thanks, I was able 'using pdb' to fix the problem as per Edward's suggestion. -- http://mail.python.org/mailman/listinfo/python-list
copying files into one
Hi I am looping through a directory and appending all the files in one huge file, the codes below should give the same end results but are not, I don't understand why the first code is not doing it. thanks combined = open(outputFile, 'wb') for name in flist: if os.path.isdir(file): continue infile = open(os.path.join(file), 'rb') # CODE 1 this does not work tx = infile.read(1000) if tx == : break combined.write(tx) infile.close() # CODE 2 but this works fine for line in infile: combined.write(line) infile.close() combined.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: copying files into one
Gary Wessle wrote: I am looping through a directory and appending all the files in one huge file, the codes below should give the same end results but are not, I don't understand why the first code is not doing it. combined = open(outputFile, 'wb') for name in flist: if os.path.isdir(file): continue infile = open(os.path.join(file), 'rb') this shouldn't work. 'file' is a type object, not a filename. did you rebind 'file' without showing us? or should those 'file's be 'name's instead? in either case, calling os.path.join with one argument is pointless. # CODE 1 this does not work tx = infile.read(1000) if tx == : break combined.write(tx) infile.close() hint: where will break take you? therein lies your answer. -- Edward Elliott UC Berkeley School of Law (Boalt Hall) complangpython at eddeye dot net -- http://mail.python.org/mailman/listinfo/python-list
Re: copying files into one
Gary Wessle wrote: I am looping through a directory and appending all the files in one huge file, the codes below should give the same end results but are not, I don't understand why the first code is not doing it. another bit of friendly advice (for others as well): learn to use pdb before posting your code for the group to debug. this type of error is easily catchable with a little effort. you'll learn more by investigating your problems yourself before feeding them to the great distributed debugger known as comp.lang.python. -- Edward Elliott UC Berkeley School of Law (Boalt Hall) complangpython at eddeye dot net -- http://mail.python.org/mailman/listinfo/python-list