Advertisement

06.16.2008 at 01:38AM PDT, ID: 23487400
[x]
Attachment Details

Appending file contents to a new file

Asked by sara_bellum in Python Scripting Language, Linux, Linux Programming

Tags: Python, Absolute beginner, appending file contents to a new file

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
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
#! /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()
[+][-]06.16.2008 at 01:43AM PDT, ID: 21791723

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 02:09AM PDT, ID: 21791845

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Python Scripting Language, Linux, Linux Programming
Tags: Python, Absolute beginner, appending file contents to a new file
Sign Up Now!
Solution Provided By: RichieHindle
Participating Experts: 3
Solution Grade: A
 
 
[+][-]06.16.2008 at 07:35AM PDT, ID: 21794000

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 05:11PM PDT, ID: 21798627

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628