copying files into one

G

Gary Wessle

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()
 
E

Edward Elliott

Gary said:
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.
 
E

Edward Elliott

Gary said:
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.
 
T

Ten

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,241
Latest member
PorterShor

Latest Threads

Top