473,411 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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

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 itto 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
cx******@sentara.com
Jun 27 '08 #1
2 1186
CRAIG DALTON wrote:
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
cx******@sentara.com

I don't think what you pasted will work at all. The r+ should produce a syntax
error on the g=open(os.path.join(r+,files) line.

No real reason to get shutil involved in this.

try something like this (not tested):

outfp=open("c:\\output\\testing1.txt", 'w')
for root,dirs,files in os.walk("c:\\test"):
for fname in files:
if fname.endswith(".txt"):
infp=open(os.path.join(root, fname), 'r')
of.write(infp.read())
infp.close()

outfp.close()

or if the files are binary:

use 'wb' and 'rb' instead of 'w' and 'r'

BTW - This will concatenate ALL the .TXT files in c:\test as well as any files
in any subfolders into a single output file.

-Larry
Jun 27 '08 #2
En Fri, 02 May 2008 18:39:36 -0300, Larry Bates <la*********@websafe.com`>
escribió:
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.

--
Gabriel Genellina

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Tuang | last post by:
Does anyone (esp. Alex, if you're listening) know if there is an update of Python in a Nutshell coming for Python 2.4?
7
by: Helmut Jarausch | last post by:
Hi, is there a new edition of "Python in a Nutshell" covering Python 2.5 coming soon? Many thanks, Helmut Jarausch Lehrstuhl fuer Numerische Mathematik
0
by: balu | last post by:
computer language python in 2007 http://www.bidvertiser.com/ http://bigchurch.com/go/g906803-pmem http://indianfriendfinder.com/go/g906803-pmem
0
by: lrajselj | last post by:
Hi. I have a problem with presentation of numbers in Python 1.5.2 that is integrated in Telit module GE863-GPS. The problem is, that Python does not recognize decimal numbers (For example: I...
3
by: yps | last post by:
as a new learner of python,which book in <python in a nutshelland <programming pythonis more suitable? and recommend several books? thanks best regards pase.Y
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.