简体   繁体   中英

compare two list of files between different directories in python

I am trying to compare two list of files from different directories. If there is a match found, the file should be written in a different directory. Below is my code.

filelist= ['sample2\\output_1.txt','sample2\\output_2.txt','sample3\\asn_todlx_mf_output_3.txt']
filelist2 = ['sample\\output_1.txt','sample\\output_3.txt','sample\\output_7.txt','sample\\output_2.txt','sample1\\output_3.txt']
a = 1
for name in filelist:
    a = a + 1 
    for x in filelist2 :
        file1 = open(x, 'r')
        file2 = open(name,'r')
        FO = open('right\\right_file'+str(a)+'.txt', 'w')

        for line1 in file1:
            for line2 in file2:
                if line1 == line2:
                    FO.write("%s\n" %(line1))

        FO.close()
        file1.close()
        file2.close()

For instance, output1 from 'sample folder(filelist)' is compared with every files in 'sample2(filelist)', if there is match, it should be written 'right' folder like 'right_file1.txt'.But the script is generating 15 files starting from 'right_file1.txt' to 'right_file15.txt'. It works good when I tried to compare one file with the list of files. Please help me, getting this.

That's how i would do it.

filelist1   = ['sample2\\output_1.txt','sample2\\output_2.txt','sample3\\asn_todlx_mf_output_3.txt']
filelist2   = ['sample\\output_1.txt','sample\\output_3.txt','sample\\output_7.txt','sample\\output_2.txt','sample1\\output_3.txt']

dir1 = filelist1[0].split('\\')[:-1]
filelist1 = [x.split('\\')[-1] for x in filelist1]

dir2 = filelist2[0].split('\\')[:-1]
filelist2 = [x.split('\\')[-1] for x in filelist2]

common = [x for x in filelist1 if x in filelist2]

print(common)
# ['output_1.txt', 'output_2.txt']

a = 1
for file in common:
    a += 1 
    with open(dir1 + '\\' + file) as f_in:
        contents = f_in.readlines()
        with open('right\\right_file' + str(a) + '.txt', 'w') as f_out:
            f_out.write(contents)

Initially i look for the files that are common between the two lists and i store their names in common . Then for all files in the common list i create their copy in this other directory as you mentioned. Notice the use of with which handles the closing and flushing of the files. Use that instead of manually managing the file unless you have a reason not to.

Finally, i did not get the logic behind your iterator a but i just copied it from you. It starts with value 2! If you want to grab the number from the file copied you have to go about doing it differently. Your way makes the origin of the created file untraceable..

Let me know if that worked for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM