user3072782
user3072782

Reputation: 49

Files are not merging : python

I want to merge the contents of two files into one new output file .

I have read other threads about merging file contents and I tried several options but I only get one file in my output. Here's one of the codes that I tried and I can't see anything wrong with it.

I only get one file in my output and even if I switch position of file1 and file2 in the list, i still only get only file1 in my output.

Here is my Code :

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

How can i do this ?

My whole code that leads to merging to these two files

source1 = open('A','r')
output = open('file1','w')
output.write(',yes.\n'.join(','.join(line) for line in source1.read().split('\n')))

source1 = open('B', 'r')
output = open('file2','w')
output.write(',no.\n'.join(','.join(line) for line in source2.read().split('\n')))

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

Upvotes: 0

Views: 173

Answers (3)

Burhan Khalid
Burhan Khalid

Reputation: 174698

You can just combine your read and writes into one with statement (if you don't really need the intermediary files); this will also solve your closing problem:

with open('A') as a, open('B') as b, open('out.txt','w') as out:
   for line in a:
       out.write(',yes.\n'.join(','.join(line)))
   for line in b:
       out.write(',no.\n'.join(','.join(line)))

Upvotes: 1

justhalf
justhalf

Reputation: 9117

After the edit it's clear where your mistake is. You need to close (or flush) the file after writing, before it can be read by the same code.

source1 = open('A','r')
output = open('file1','w')
output.write(',yes.\n'.join(','.join(line) for line in source1.read().split('\n')))
output.close()

source2 = open('B', 'r')
output = open('file2','w')
output.write(',no.\n'.join(','.join(line) for line in source2.read().split('\n')))
output.close()

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

The reason why the first file is available is because you remove the reference to the file descriptor of file1 by reassigning the variable output to hold the file descriptor for file2, and it will be closed automatically by Python.

As @gnibbler suggested, it's best to use with statements to avoid this kind of problem in the future. You should enclose the source1, source2, and output in a with statement, as you did for the last part.

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304413

The first file is being closed/flushed when you rebind output to a new file. This is the behaviour of CPython, but it's not good to rely on it

Use context managers to make sure that the files are flushed (and closed) properly before you try to read from them

with open('A','r') as source1, open('file1','w') as output:
    output.write(',yes.\n'.join(','.join(line) for line in source1.read().split('\n')))

with open('B','r') as source2, open('file2','w') as output:
    output.write(',no.\n'.join(','.join(line) for line in source2.read().split('\n')))

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            print("Reading from: " + fname)
            data = infile.read()
            print(len(data))
            outfile.write(data)

There is a fair bit of duplication in the first two blocks. Maybe you can use a function there.

Upvotes: 1

Related Questions