You can try using os.listdir()
: https://www.geeksforgeeks.org/python-os-listdir-method/
os.listdir() method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then list of files and directories in the current working directory will be returned.
import os
old_directory = os.listdir("path/to/old/directory")
new_directory = os.listdir("path/to/new/directory")
Next we need to compare the directories to see which files have not been moved over to the new directory. If we know that all files in the "new" directory will have the suffix -updated
such as old-filename-updated.ext
, then we can remove -updated
from each file name string in new_directory
so that the names match what is in old_directory
, allowing an easier comparison to find what is missing.
import os
old_directory = os.listdir("path/to/old/directory")
new_directory = os.listdir("path/to/new/directory")
# Use list comprehension to modify each element in new directory and remove "-updated"
new_directory_modified = [element.replace("-updated", "") for element in new_directory]
Now we can find the missing files using set arithmetic:
import os
old_directory = os.listdir("path/to/old/directory")
new_directory = os.listdir("path/to/new/directory")
# Use list comprehension to modify each element in new directory and remove "-updated"
new_directory_modified = [element.replace("-updated", "") for element in new_directory]
# Using `set` is faster than using `in` to find missing elements
missing_elements = list(set(old_directory) - set(new_directory_modified))
From here, all you need to do is iterate over this list, add -updated
to each element, and save into the new directory.