4

I have a script that copies files from one folder structure to another folder with different structure.

ex. Folder 1
    c.txt
    d.txt
    subdir1
      a.txt
      b.txt

Script copies files/dirs from Folder 1 to Folder 2(Folder 2 has different structure) in this format

Folder 2
   subdir2
     c.txt
     d.txt
   subdir1
     a.txt
     b.txt

In folder 2 i can create my own files like new1.txt, new2.txt. After creating new files the folder 2 structure will be something like this:

Folder 2
   new1.txt
   new2.txt
   subdir2
     c.txt
     d.txt
   subdir1
     a.txt
     b.txt

Now i need to compare the directory content of Folder1 and Folder2. I am using filecmp.dircmp for comparing directory content. filecmp.dircmp on these two folders will give me subdir2, subdir2/c.txt, subdir2/d.txt as new directory and new files with new1.txt and new2.txt. But actually i have only created new1.txt and new2.txt as new files and the other files only copied from one folder to different folder.

As a new files i need only new1.txt and new2.txt in result. Any idea how can i do it. I am writing this code in python

user1495649
  • 53
  • 1
  • 3
  • 2
    So essentially you want to ignore the subfolder structure completely and just compare filenames? Could there ever be duplicate filenames in different directories? What then? Have you looked at the docs for [`os.walk()`](http://docs.python.org/library/os.html#os.walk)? – Tim Pietzcker Jul 02 '12 at 10:25

1 Answers1

3

Tim is right, os.walk is the solution:

>>> path = r'D:\DATA\FP12210\My Documents\Temp\Python'
>>> import os
>>> files1 = []
>>> for root, dirs, files in os.walk(os.path.join(path, 'Folder 1')):
    files1.extend(files)


>>> files1
['c.txt', 'd.txt', 'a.txt', 'b.txt']
>>> files2 = []
>>> for root, dirs, files in os.walk(os.path.join(path, 'Folder 2')):
    files2.extend(files)


>>> files2
['new1.txt', 'new2.txt', 'a.txt', 'b.txt', 'c.txt', 'd.txt']

Then you can diff your outputs:

>>> print [f for f in files2 if f not in files1]
['new1.txt', 'new2.txt']
Emmanuel
  • 12,525
  • 9
  • 44
  • 66