473,597 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(f ile): continue

infile = open(os.path.jo in(file), 'rb')

# CODE 1 this does not work
tx = infile.read(100 0)
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( )
May 14 '06 #1
4 984
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(f ile): continue
infile = open(os.path.jo in(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(100 0)
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
May 14 '06 #2
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.pytho n.

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 14 '06 #3
Ten
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(f ile): 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.jo in(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(100 0)
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.
May 14 '06 #4

thanks, I was able 'using pdb' to fix the problem as per Edward's
suggestion.

May 14 '06 #5

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

Similar topics

3
3633
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore my settings if my profile became tampered with or corrupt. Is there any sample code available out there? -Robert
0
1529
by: Dan | last post by:
We debug a VB.Net App that uses compact framework to a DAP Windows CE.Net device. When I debug I would like to only have the EXE copied down. Right now it copies any referrences as well and checks the .NET CF version. The first message I receive is: Files in the package 'netcf.all.wce4.armv4.cab' are more recent on the device than on the development computer. Consider upgrading to the latest version. Files affected are:
4
1457
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore my settings if my profile became tampered with or corrupt. Is there any sample code available out there? -Robert
0
249
by: Dan | last post by:
We debug a VB.Net App that uses compact framework to a DAP Windows CE.Net device. When I debug I would like to only have the EXE copied down. Right now it copies any referrences as well and checks the .NET CF version. The first message I receive is: Files in the package 'netcf.all.wce4.armv4.cab' are more recent on the device than on the development computer. Consider upgrading to the latest version. Files affected are:
6
4965
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar, or i couldn't find sth which give totalbytes/written bytes class. And does system.io.file class provide awaring of the chunks / bytes of the files bytes which are written?
0
8267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6681
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5844
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3880
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2394
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1229
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.