0

I have two folders "Old_2014" and "New_2015" both folder has files such as

Old_2014=abl_results 
         abc_results1
New_2015=abl_results
         abcd_results1

old_2014/abl_results-> has 21 items new_2014/abl_results-> has 165 items

I want to compare these files and copy the Unique records in new folder similarly i have total 103 files name abc, abcd,abcde etc. eg:- I need to find the files which are there in the old but not in the new and at the end the, the results file need to have the unique value

3
  • Diff will just compare, I need to know if any files has been added or removed? Commented Jul 21, 2015 at 9:56
  • Like both the files has same name, so let say if there is any new file, that also i want to keep Commented Jul 21, 2015 at 9:58
  • Note: your question is attracting close-votes for being too broad. Since you're a new user, you might want to check out the "how to ask a good question" guidelines, and note that you are expected to post your attempt to solve the problem. Commented Jul 21, 2015 at 10:50

1 Answer 1

1

Here's code that does what you asked for:

import os

old_dir = "old_d"
new_dir = "new_d"

files_old = os.listdir(old_dir)
files_new = os.listdir(new_dir)

print "In old not in new:", list(set(files_old) - set(files_new))

Specifically: it lists files that are in the old that are not in the new.

~ mgregory$ mkdir old_d
~ mgregory$ mkdir new_d
~ mgregory$ touch old_d/1.txt
~ mgregory$ touch old_d/2.txt
~ mgregory$ touch new_d/1.txt
~ mgregory$ python foo.py
In old not in new: ['2.txt']
~ mgregory$ 

Things to note:

  • You can get the list of files in a directory as a list using listdir()
  • You can do interesting set operations with lists by converting them to sets
  • If you wanted to know what is in new that is not in old, you should look up the doco on sets

Another note: enrico's answer is also correct:

~ mgregory$ diff old_d new_d
Only in old_d: 2.txt
~ mgregory$ 

diff does a good job of diffing directories.

Sign up to request clarification or add additional context in comments.

5 Comments

Inside old= abc_reulsts there are xml's eg:- 1.xml, 2.xml, 3.xml new = 5.xml so the output should be 5.xml
I have no idea what you are talking about. This is the first time you've mentioned xml files. Please see the advice for asking good questions, and follow it.
I am just asking i have a folder name old_2014 which has a file abc_results inside abc_results i have some xml files, on the other hand new_2015 also has the abc_results and it has some xml files so now i want to compare these two xml
If you have a new question, you should ask a new question :)
If you have a new question, you should ask a new question. Otherwise you render the answers given previously as useless, which is not a good thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.