I want to create a script to clone a folder from the directory “../x/A/” to “../y/A/” and remove files from the directory “../y/A/” that are not in “../x /A/”.
I wrote the delete part. Is it possible to optimize this somehow?
I would also like to know if there are ready-made solutions for what I want?
from pathlib import Path
import os
directory_path1 = 'E:**path01'
directory_path2 = 'E:**path2_path01'
base_fold = Path(directory_path1).name
local_delete_list = []
#absolute_delete_list = []
def get_files_paths(directory, mode = False):
directory = Path(directory)
if mode == True:
files_paths = [file_path.parent.name +'/' +file_path.name for file_path in directory.rglob('*') if file_path.is_file()]
else:
files_paths = [file_path for file_path in directory.rglob('*') if file_path.is_file()]
return files_paths
absolute_list_path1 = tuple([str(x) for x in tuple(get_files_paths(directory_path1))])
absolute_list_path2 = tuple([str(x) for x in tuple(get_files_paths(directory_path2))])
local_list_path1 = []
local_list_path2 = []
for i in absolute_list_path1:
local_list_path1.append(i.replace(i[0:i.index(str(base_fold))],''))
for i in absolute_list_path2:
local_list_path2.append(i.replace(i[0:i.index(str(base_fold))],''))
for path in local_list_path2:
if path not in local_list_path1:
local_delete_list.append(path)
for i in absolute_list_path2:
for j in local_delete_list:
print(i, ' || ', j)
try:
if i.index(j):
#print(i,' -match found- ' , j)
os.remove(i)
except:
print("no matches")
I wrote the delete part. Is it possible to optimize this somehow?
I would also like to know if there are ready-made solutions for what I want?
Lord Genocide is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.