dimanche 31 janvier 2016

Comparing a file to another file

import sys
import os
import glob
directorylist = ["A", "B", "C", "D"]

for directory in directorylist:
    for file in glob.glob(os.path.join(directory, "tobecompared(*).txt")):
        with open(file) as f:
                a=0
                fdata = f.readlines()
                for line in fdata:
                    with open ("/Users/Student/Desktop/folder/pool.txt", "r") as pool:
                        if line in pool:
                            a=a+1
                print a

I want to compare lines in text files (tobecompared1.txt, tobecompared2.txt) to the lines in another text file (pool.txt). The folder contains the folders A, B, C, and D. The pool.txt is in the folder while the tobecompared(*).txt files are in the directories A, B, C. This code aims to print the no. of lines in the tobecompared text files that are present in the original pool.

This code prints 0 repeatedly, so I'm guessing there is an issue with the loops that increases the value of a by 1. What is really aimed at is:

Say the pool.txt is

A
B
C
D

tobecompared(01).txt in A is

A
A
F
H
C
B

tobecompared(02).txt in A is

A
B
C

tobecompared(01).txt in B is

G
D
C

The output would be:

4
3
2

Aucun commentaire:

Enregistrer un commentaire