call definite folder name and value in dictionary in Python -
i want write function copy structure , content of 1 root folder 1 (that contains of files in initial folder , function must compare files , directories , copy if not exists in destination folder). name of folders dict.values().
def copy_tran(reg_dir): z_folders = os.listdir(reg_dir) png_folder = r'c:\users\tei\desktop\temp' png_subfolders = os.listdir(png_folder) dir in z_folders: pngdir in png_subfolders: path_dir2 = os.path.join(png_folder, pngdir) path_dir = os.path.join(reg_dir, dir) transp_basename = os.path.basename(path_dir)[4:] png_basename = os.path.basename(path_dir2)[4:] if transp_basename == png_basename: region in dict.values(): if not os.path.exists(png_folder + '\\' + 'reg_' + ('%s' % (region))): os.mkdir(png_folder + '\\' + 'reg_' + ('%s' % (region))) list_zooms = ['8', '9', '10', '11'] zoom in list_zooms: if not os.path.exists(png_folder + '\\' + 'reg_' + ('%s' % (region)) + '\\' + ( '%s' % (zoom))): os.mkdir(png_folder + '\\' + 'reg_' + ('%s' % (region)) + '\\' + ('%s' % (zoom))) x_path = os.path.join(path_dir, zoom) list_x = os.listdir(x_path) y in list_x: y_path = os.path.join(x_path, y) if not os.path.exists(png_folder + '\\' + 'reg_' + ('%s' % (region)) + '\\' + ( '%s' % (zoom)) + '\\' + y): os.mkdir(png_folder + '\\' + 'reg_' + ('%s' % (region)) + '\\' + ('%s' % (zoom)) + '\\' + y)
the problem when comparing folder's names, python takes random (as see) folder in root folder (reg_centrsib_north) although in list (2nd line) second element. so, function creates wrong folders (in destination folder reg_center function create subfolders folder reg_centrsib_north). way need rewrite code calls folders of same name (that dict.values())? i'm puzzled, ideas?
try simple solution problem based on shutil.copy2
def copydir(src, dst, symlinks=false, ignore=none): if not os.path.exists(dst): os.makedirs(dst) item in os.listdir(src): s = os.path.join(src, item) d = os.path.join(dst, item) if os.path.isdir(s): copydir(s, d, symlinks, ignore) else: if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1: shutil.copy2(s, d)
this recursive implementation:
- check if destination folder exists, if not create it
- loop on every element of source folder
- if folder call function on , goto 1
- if file check if exists , if modified: based on copy
this should jsut copy not esist or has been modified
Comments
Post a Comment