Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Move and overwrite files and folders

I have a directory, 'Dst Directory', which has files and folders in it and I have 'src Directory' which also has files and folders in it. What I want to do is move the contents of 'src Directory' to 'Dst Directory' and overwrite anyfiles that exist with the same name. So for example 'Src Directory\file.txt' needs to be moved to 'Dst Directory\' and overwrite the existing file.txt. The same applies for some folders, moving a folder and merging the contents with the same folder in 'dst directory'

I'm currently using shutil.move to move the contents of src to dst but it won't do it if the files already exist and it won't merge folders; it'll just put the folder inside the existing folder.

Update: To make things a bit clearer, what I'm doing is unzipping an archive to the Dst Directory and then moving the contents of Src Directory there and rezipping, effectively updating files in the zip archive. This will be repeated for adding new files or new versions of files etc which is why it needs to overwrite and merge

Solved: I solved my problem by using distutils.dir_util.copy_tree(src, dst), this copies the folders and files from src directory to dst directory and overwrites/merges where neccesary. Hope that helps some people!

Hope that makes sense, thanks!

like image 961
Artharos Avatar asked Sep 14 '11 16:09

Artharos


People also ask

How do you overwrite an existing file in Python?

To overwrite a file, to write new content into a file, we have to open our file in “w” mode, which is the write mode. It will delete the existing content from a file first; then, we can write new content and save it. We have a new file with the name “myFile. txt”.

Will Shutil move overwrite?

If the filename is included in the destination path (relative or absolute) shutil will overwrite.

Does Copy_tree overwrite?

copy_tree . It works just fine and you don't have to pass every argument, only src and dst are mandatory. However in your case you can't use a similar tool like shutil. copytree because it behaves differently: as the destination directory must not exist this function can't be used for overwriting its contents.


1 Answers

This will go through the source directory, create any directories that do not already exist in destination directory, and move files from source to the destination directory:

import os import shutil  root_src_dir = 'Src Directory\\' root_dst_dir = 'Dst Directory\\'  for src_dir, dirs, files in os.walk(root_src_dir):     dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)     if not os.path.exists(dst_dir):         os.makedirs(dst_dir)     for file_ in files:         src_file = os.path.join(src_dir, file_)         dst_file = os.path.join(dst_dir, file_)         if os.path.exists(dst_file):             # in case of the src and dst are the same file             if os.path.samefile(src_file, dst_file):                 continue             os.remove(dst_file)         shutil.move(src_file, dst_dir) 

Any pre-existing files will be removed first (via os.remove) before being replace by the corresponding source file. Any files or directories that already exist in the destination but not in the source will remain untouched.

like image 92
Ray Avatar answered Oct 06 '22 07:10

Ray