Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Appending file contents to a new file

Posted on 2008-06-16
5
Medium Priority
?
2,407 Views
Last Modified: 2012-05-05
I'm just starting out with Python but I'm stuck already and can't proceed unless I understand how this works.  I'm trying to append the contents of two files to a new file.  Each of the two source files has only a few simple lines of text (words separated by spaces) with Unix line returns, but no commas or other delimiters (I will try to work with more complex files later).  

I understand that these strings are read in as lists but cannot find a way to print lists that are read from a file. I tried using "for line in src1:" followed by a print statement but got a syntax error.  My most recent attempt at a script is attached below.  The errors are:  

File "./test.py", line 11, in ?
    print src1.read(output1)
TypeError: an integer is required

File "./test.py", line 12, in ?
    src1.write(output1)
TypeError: argument 1 must be string or read-only character buffer, not list

I don't think I should need to define a method unless I'm trying to strip quotation marks, line returns or white spaces.  Let me know what I'm missing, thanks

#! /usr/bin/python
 
import string, sys, fileinput
 
src1=open('my_src1.txt', 'r')
output1 = src1.readlines()
src2=open('my_src2.txt', 'r')
output2 = src2.readlines()
dest = open( 'my_dest.txt', 'a' )
print src1.read(output1)  
src1.write(output1)
src1.close()
print src2.read(output2)
src2.write(output2)
src2.close()
dest.close()

Open in new window

0
Comment
Question by:sara_bellum
5 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 21791723
print src1.read(output1)  
How should that work?

and well if you have a list you have to walk the list and print the contents line by line or you may look is there's something like write_lines or the like to output a complete list.

Regards
Friedrich
0
 
LVL 14

Accepted Solution

by:
RichieHindle earned 2000 total points
ID: 21791845
Here you go.

Note that the code really does *append* to my_dest.txt, so if you run it twice you get my_src1.txt+my_src2.txt+my_src1.txt+my_src2.txt in my_dest.txt.

If you want each run to empty my_dest.txt before writing to it, change open('my_dest.txt', 'a') to open('my_dest.txt', 'w').

#! /usr/bin/python
 
import string, sys, fileinput
 
src1=open('my_src1.txt', 'r')
output1 = src1.read()
src1.close()
 
src2=open('my_src2.txt', 'r')
output2 = src2.read()
src2.close()
 
dest = open('my_dest.txt', 'a')
dest.write(output1)  
dest.write(output2)
dest.close()

Open in new window

0
 
LVL 17

Expert Comment

by:ramrom
ID: 21794000
print src1.read(output1) is a pretty confused expression

You already read the file contents into output1. All you need is print output1.

src1.read() would return an empty string, since src1 is at end of file.

If you pass a parameter to read() it must be an integer, since that parameter is the # of bytes to read.

Also aim (in future questions) to omit inessential information (in this case the discussion of what is in the files).

I also suggest you read the tutorials in the topic area (in this case files) you are working in. The explanations and examples should help you get it right the first time.

Of course we are glad to help.
0
 

Author Closing Comment

by:sara_bellum
ID: 31467497
Thanks!  I had read a number of tutorials, but the more I read, the more confused I got, so I could no longer see what information you really needed - it was good to just see an answer that works
0
 

Author Comment

by:sara_bellum
ID: 21798627
I ended up using a slightly modified form of the answer which I post here for reference / in case it helps anyone.  This does add annoying ^M line returns, but I can remove those with sed.
#! /usr/bin/python
 
import string, sys, fileinput
 
src1 = open('header.dat', 'r')
output1 = src1.read()
src1.close()
 
src2 = open('sample.dat', 'r')
output2 = src2.readlines()
src2.close()
del output2[0:3+1]
 
dest = open('sample_form.dat', 'w')
dest.write(output1)  
dest.writelines(output2)
dest.close()

Open in new window

0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question