Need help: Compiling Python-Code &In-Reply-To=

C

CRAIG DALTON

Hi,

I'm looking to append several text files in one director and out put the combined files into another director. I'm new to Python and just can't get it to work. So far I've been able to create a file in the desired directory but it isn't pulling any of the data in the originating directory. Could please look at my code and tell me what I'm doing wrong.

import os,shutil
f=open("c:\\output\\testing1.txt","a+")
for r,d,fi in os.walk("c:\\test"):
for files in fi:
if files.endswith(".txt"):
g=open(os.path.join(r+,files))
shutil.copyfileobj(g,f)
g.close()
f.close()

Any help would be great.

Thanks,

Craig Dalton
Business Applications Systems Analyst
Sentara Healthcare Systems
Information Technology
(e-mail address removed)
 
G

Gabriel Genellina

No real reason to get shutil involved in this.

....except memory usage; shutil.copyfileobj reads the input in chunks, but
your version reads the whole file in memory. Depending on the file sizes,
this might be an issue.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top