473,386 Members | 1,775 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,386 software developers and data experts.

append one file to another

Hi,

I want to append one (huge) file to another (huge) file. The current
way I'm doing it is to do something like:

infile = open (infilename, 'r')
filestr = infile.read()
outfile = open(outfilename, 'a')
outfile.write(filestr)

I wonder if there is a more efficient way doing this?
Thanks.

Jul 21 '05 #1
8 9856
Am Tue, 12 Jul 2005 06:47:50 -0700 schrieb b8*******@yahoo.com:
Hi,

I want to append one (huge) file to another (huge) file. The current
way I'm doing it is to do something like:

infile = open (infilename, 'r')
filestr = infile.read()
outfile = open(outfilename, 'a')
outfile.write(filestr)

I wonder if there is a more efficient way doing this?
Thanks.


I guess (don't know), that this is faster:

for line in infile:
outfile.write(line)

At least if this a file with "lines".

If it is a binary file, you could read
N bytes at once: infile.read(N)

Thomas
--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 21 '05 #2
Thanks for the nice suggestions!

As a side question, you mentioned opening files in binary mode, in case
the code needs to run under Windows or cross-platform. What would
happen otherwise? Is it an issue of big little endian or some other
issue?

Jul 21 '05 #3
Dear me, replying to myself twice in one day...

On Wed, 13 Jul 2005 00:39:14 +1000, Steven D'Aprano wrote:
Then, if you are concerned that the files really are huge, that is, as big
or bigger than the free memory your computer has, read and write them in
chunks:

data = infile.read(64) # 64 bytes at a time is a bit small...
outfile.write(data)


Sorry, that should be in a loop:

data = "anything"
while data:
data = infile.read(64) # data will be empty when the file is read
outfile.write(data)

--
Steven.
Jul 21 '05 #4
On Tue, 12 Jul 2005 06:47:50 -0700, b8*******@yahoo.com wrote:
Hi,

I want to append one (huge) file to another (huge) file.
What do you call huge? What you or I think of as huge is not necessarily
huge to your computer.
The current
way I'm doing it is to do something like:

infile = open (infilename, 'r')
filestr = infile.read()
outfile = open(outfilename, 'a')
outfile.write(filestr)

I wonder if there is a more efficient way doing this?


Why? Is it not working? Is it too slow? Does it crash your computer?

If you have any expectation that you code needs to run under Windows, or
cross-platform, or contains binary data, you should open your files in
binary mode:

infile = open(infilename, 'rb')
outfile = open(outfilename, 'ab')

For raw copying, you should probably use binary mode even if they just
contain text. Better safe than sorry...

Then, if you are concerned that the files really are huge, that is, as big
or bigger than the free memory your computer has, read and write them in
chunks:

data = infile.read(64) # 64 bytes at a time is a bit small...
outfile.write(data)

Instead of 64 bytes, you should pick a more realistic figure, which will
depend on how much free memory your computer has. I suppose a megabyte is
probably reasonable, but you will need to experiment to find out.

Then when you are done, close the files:

infile.close()
outfile.close()

This is not strictly necessary, but it is good practice. If your program
dies, the files may not be closed properly and you could end up losing
data.
--
Steven.

Jul 21 '05 #5
Its been a while since I last coded in Python, so please make sure you test
it before trying it so you don't clobber your existing file. Although it may
not be more effecient than what you are doing now or has been suggested
already, it sure cuts down on the typing.

open(outfilename,'a').write(open(infilename).read( ))

Regards.

<b8*******@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,

I want to append one (huge) file to another (huge) file. The current
way I'm doing it is to do something like:

infile = open (infilename, 'r')
filestr = infile.read()
outfile = open(outfilename, 'a')
outfile.write(filestr)

I wonder if there is a more efficient way doing this?
Thanks.

Jul 21 '05 #6
On 2005-07-12, b8*******@yahoo.com <b8*******@yahoo.com> wrote:
As a side question, you mentioned opening files in binary
mode, in case the code needs to run under Windows or
cross-platform. What would happen otherwise? Is it an issue
of big little endian or some other issue?


The end-of-line characters might get converted -- even if
they're not really "end-of-line" characters in the file in
question.

--
Grant Edwards grante Yow! My mind is a potato
at field...
visi.com
Jul 21 '05 #7
On Tue, 12 Jul 2005 07:38:39 -0700, b8*******@yahoo.com wrote:
Thanks for the nice suggestions!

As a side question, you mentioned opening files in binary mode, in case
the code needs to run under Windows or cross-platform. What would
happen otherwise? Is it an issue of big little endian or some other
issue?


No, nothing to do with big and little endian issues. It is all to do with
the line delimiter, and possibly the end-of-file marker.

Windows uses '\r\n' as the line delimiter for text files. (Or is it
'\n\r'? I always forget...)

Old-style Macintosh used '\r', and (almost) everything else, including new
Macs running OS X, uses '\n'.

If you open files in text mode, there can be complications due to the
different line endings. To be perfectly frank, I only use Python under
Linux, so I don't have the foggiest idea of just what Bad Things can
happen. I know it is a big problem when using some FTP programs, which
have a tendency to destroy binary programs if you upload/download them in
text mode.

I just did some experiments here, and can't get anything bad to happen.
But whatever the problem is, my grand-pappy always told me, open the
danged file in binary mode and you can't go wrong.

*wink*

I have found some discussions here:

http://python.active-venture.com/tut/node9.html

"Windows makes a distinction between text and binary files; the
end-of-line characters in text files are automatically altered slightly
when data is read or written. This behind-the-scenes modification to file
data is fine for ASCII text files, but it'll corrupt binary data like that
in JPEGs or .EXE files. Be very careful to use binary mode when reading
and writing such files."

and here:

http://zephyrfalcon.org/labs/python_pitfalls.html

This website recommends:

"Solution: Use the correct flags -- 'r' for text mode (even on Unix), 'rb'
for binary mode."

but I've never had any problems using 'rb' for text files under Linux.

I'm also told that Windows uses ctrl-Z as the end-of-file marker, and if
it finds that character in the middle of a text file, it will assume the
file has finished and stop reading. But only in text mode, not binary. I
don't think that's a problem for Linux.
--
Steven.

Jul 21 '05 #8
b8*******@yahoo.com wrote:
Hi,

I want to append one (huge) file to another (huge) file. The current
way I'm doing it is to do something like:

infile = open (infilename, 'r')
filestr = infile.read()
outfile = open(outfilename, 'a')
outfile.write(filestr)

I wonder if there is a more efficient way doing this?
Don't wonder, like the ancient philosophers; be an empiricist :-)

Thanks.


If the files are truly huge, you run the risk of exhausting real memory
and having to swap.

Try this:
Having opened the files,

for line in infile:
outfile.write(line)

Otherwise look at the docs for read the method and check out the "size"
argument.

General warnings: (1) If you want to be portable, consider text/binary
differences. (2) Consider what to do if the last line in <outfilename>
is not terminated.
Jul 21 '05 #9

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

Similar topics

2
by: sm | last post by:
How to "Insert" (not append) new text segment to an existing text file? Assume that we have text file as shown below; Elvis Sofia Kylix BCB--> How to insert here? Atten BuilderX Roma
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
2
by: Matrix | last post by:
Greetings I want to append records in a XML file,I use XMLTextWriter,but i found it only create a XML file,not append records in exist XML file.why?...
2
by: JMCN | last post by:
hi i have a general question regarding append queries in access 97. each week i need to update my table(tblonlinereg) with new or modified records. firstly, i import the text file into my...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
14
by: vbMark | last post by:
Greetings, This seems like it should be simple but I can't figure out how to do this. I just want to append binary file 2 on to the end of binary file 1. Sample code please? Thanks!
2
by: Roy Gourgi | last post by:
Hi, How could I append a text file to another text file. Let's say that I have a File1 and I want to append File2 at the end of File1, how would I do that? TIA Roy
3
by: wvmbark | last post by:
First time poster... I just found this forum and it appears there's plenty of people here that could make short work of problem that's been driving me absolutely bonkers for months. Every day we...
3
by: Wolfgang Meister | last post by:
I would like to append text to a file which might exist or not. What is the easiest way to open or create it depending if it exists or not and to set it into an "append" text mode?
2
by: bashhead | last post by:
Hi all, I have a basic shell script question. Say I have a variable called test. I already have a value in test = 'monkey' How do I append another value taken from a file, to this variable?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...
0
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...

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.