2

Possible Duplicate:
How do I concatenate files in Python?

def copy_file(file_name,copy_file_name,copies):
    i=0
    a=open(file_name,'r')
    f=open(copy_file_name,'w')
    for line in a:
        while i<copies:
            f.write(line)
            i+=1
    a.close()
    f.close()
    return 
copy_file("D:\student\example2.txt","D:\student\copy_file_name.txt",3)

i need to copy a text file 3 times to another file and the loop stops after the first line:(

def merge_file(list,file_name):
    for i in range(len(list)):
        a=open(list[i],'r')
        f=open(file_name,'w')
        f.write(list[i])
    f.close
    a.close
    return
merge_file([("D:\student\example2.txt"),("D:\student\example3.txt")],"D:\student\copy_file_name.txt")

i need to copy list of files into one file.

Community
  • 1
  • 1
  • You may need to be more specific for what you are actually asking about. Plus, everytime you call your variable `list` you shadow a built-in function... – moooeeeep Nov 19 '12 at 19:56

5 Answers5

0

you want to append the files, using open(filename, 'a'), see also: How do you append to a file?

Community
  • 1
  • 1
Cameron Sparr
  • 3,925
  • 2
  • 22
  • 31
0

To copy the contents of one file into another file three times you can do something like this:

with open('outfile.txt','w') as outFile:
    for _ in range(3):
        with open('infile.txt','r') as inFile:
            for line in inFile:
                outFile.write(line)

To copy the contents of a list of files into another file, you can do something like this:

def merge_file(fileList, outFileName):
    with open(outFileName, 'w') as outFile:
        for fileName in fileList:
            with open(fileName, 'r') as inFile:
                for line in inFile:
                    outFile.write(line)

Both of these are untested, but they should work

Matt
  • 3,651
  • 3
  • 16
  • 35
0

Use shutil.copyfileobj to make the copy for you. Note that this approach is completely ignorant of any encoding issues and platform specific line separators across your input files. The plain byte stream will be copied.

import shutil

# the file to copy to
outfname = "D:\student\copy_file_name.txt"

# the files to copy from
infnames = ["D:\student\example2.txt", "D:\student\example3.txt"]

# the copy procedure
with open("outfile", 'w') as outfile:
    for fname in infnames:
        shutil.copyfileobj(open(fname, 'rb'), outfile)

If you want to duplicate the content of a single file for a given number of times, just make up the infnames accordingly:

# the file to copy from n_repetitions times
infnames = ["D:\student\example2.txt"] * n_repetitions

# same as above
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
0

your call to merge_file is passing a list of len 1 with the single item being a 2 tuple.

instead of what you have:

merge_file([("D:\student\example2.txt"),("D:\student\example3.txt")],"D:\student\copy_file_name.txt")

try this (I think this is what you meant:

merge_file(["D:\student\example2.txt","D:\student\example3.txt"],"D:\student\copy_file_name.txt")

I hope you can see the difference. If you are new to python and lists and tuples, I recommend some study: http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange

jrwren
  • 17,465
  • 8
  • 35
  • 56
0

If I understand correctly:

import fileinput
with open('output.txt') as fout:
    fout.writelines(fileinput.input(your_list))

Which is "take every line from the file names specified in your_list and write them to output.txt"

Jon Clements
  • 138,671
  • 33
  • 247
  • 280