Last active
November 9, 2021 10:39
-
-
Save JorgeMartinezG/fe8847434bc1308ef4c9e56a705696a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
OLD_ICA_PATH = "/Users/jorge/Desktop/SLE_adm2_Dec2016" | |
NEW_ICA_PATH = "/Users/jorge/OneDrive - World Food Programme/SLE_new/2016/Admin_2/" | |
OUTPUT_FILE = "/Users/jorge/Desktop/test.csv" | |
def read_files(path): | |
files = [ | |
[{"path": dir_path, "name": f} for f in files] | |
for dir_path, _, files in os.walk(path) | |
] | |
files = [item for sublist in files for item in sublist] | |
# Filter .DS_Store files. | |
files = [f for f in files if f != ".DS_Store"] | |
return files | |
def main(): | |
old_ica_files = read_files(OLD_ICA_PATH) | |
new_ica_files = read_files(NEW_ICA_PATH) | |
rows = [] | |
for ica_file in old_ica_files: | |
file_name = ica_file.get("name") | |
old_path = ica_file.get("path") | |
# Loop to find where the file has been moved. | |
new_ica_file = next((f for f in new_ica_files if f.get("name") == file_name), None) | |
if new_ica_file is None: | |
new_path = "" | |
else: | |
new_path = new_ica_file.get("path") | |
row = {"name": file_name, "old_path": old_path, "new_path": new_path} | |
rows.append(row) | |
sorted_rows = sorted(rows, key=lambda x: x["new_path"]) | |
keys = sorted_rows[0].keys() | |
with open(OUTPUT_FILE, "w", newline="") as output_file: | |
dict_writer = csv.DictWriter(output_file, keys) | |
dict_writer.writeheader() | |
dict_writer.writerows(sorted_rows) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment