1

I have 2 folders (Ex1, Ex2), where folder Ex1 contains 2500 files and folder Ex2 contains 2300 files. Some of the files in folder Ex2 contains same content as of folder Ex1, I am trying to:

  • Compare the title of files in both the directories
  • Compare the content from each individual file from both directories
  • Compare the size[in bytes] of individual file in both directories.

If the above condition matches, the matched files are to be renamed with prefix "copied" followed by the file name in folder Ex2.

If the conditions do not match, the files are to be moved to folder Ex2 and renamed with prefix "new" followed by the file name.

So far, I have written the code to match individual file names in both directories. However, upon doing several research and correct syntax, I am failing to execute the program.

I assume the approach is not valid to solve this problem. Would anyone be kind enough to suggest a better approach and provide assistance to solve this problem?

## Code for files in example1
path = "C:/Users/OneDrive/Example1"
dir_list = os.listdir(path)
print(dir_list)

## code for files in example2
path2 = "C:/Users/OneDrive/Example2"
dir_list2 = os.listdir(path)
print(dir_list2)

comp2=None

#Iterate through the files in both the folder
for f in dir_list:
    for h in dir_list2:
        if comp2 = [filecmp.cmp(f,h, shallow= true)] is True:
            print(comp2)
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

Here's valid syntax that does what I think you intended in that section of the code. It makes use of an assignment expression, something that was introduced in Python 3.8.

...

#Iterate through the files in both the folder
for f in dir_list:
    for h in dir_list2:
        if (comp2 := filecmp.cmp(f, h, shallow=True)):
            print(comp2)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • It still says invalid syntax –  May 30 '22 at 13:55
  • Assuming you have at least Python v3.8, it might be the fact that the boolean `True` was misspelled. – martineau May 30 '22 at 13:59
  • I checked, and the syntax is correct. Would you know any other way to check the file titles? –  May 30 '22 at 14:18
  • If the syntax is correct (now), why do you need another way? – martineau May 30 '22 at 14:21
  • I checked the syntax on python documentation, and my method is supposed to be correct, however, when running it says the syntax is invalid.. And also, I tried using os.path.basename(variablename) method and it does read the name, so I think that resolves my first part of the question –  May 30 '22 at 14:24
  • The problem of getting the `SyntaxError` when running the code could be because your IDE is misconfigured and not using the right version of Python. That code snippet looks like it might work — what do you mean by "…it kind does what I want"? – martineau May 30 '22 at 14:33
  • To do what you want another way, you would need to basically reimplement what `filecmp` does. – martineau May 30 '22 at 14:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245187/discussion-between-smit-shah-and-martineau). –  May 30 '22 at 16:50