Python | Move or Copy Files and Directories

Let’s say we want to copy or move files and directories around, but don’t want to do it by calling out to shell commands. The shutil module has portable implementations of functions for copying files and directories.

Code #1 : Using shutil module

filter_none

edit
close

play_arrow

link
brightness_4
code

import shutil
  
# Copy src to dst. (cp src dst)
shutil.copy(src, dst)
  
# Copy files, but preserve metadata (cp -p src dst)
shutil.copy2(src, dst)
  
# Copy directory tree (cp -R src dst)
shutil.copytree(src, dst)
  
# Move src to dst (mv src dst)
shutil.move(src, dst)

chevron_right


The arguments to these functions are all strings supplying file or directory names. The underlying semantics tries to emulate that of similar Unix commands, as shown in the comments. By default, symbolic links are followed by these commands. For example, if the source file is a symbolic link, then the destination file will be a copy of the file the link points to.

To copy the symbolic link instead, supply the follow_symlinks keyword argument as shown in the code below:

Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

shutil.copy2(src, dst, follow_symlinks = False)
  
# To preserve symbolic links in copied directories
shutil.copytree(src, dst, symlinks = True)

chevron_right


The copytree() optionally allows to ignore certain files and directories during the copy process. To do this, supply an ignore function that takes a directory name and filename listing as input, and returns a list of names to ignore as a result. The example is shown in the code below –

Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

def ignore_pyc_files(dirname, filenames):
    return [name in filenames if name.endswith('.pyc')]
  
shutil.copytree(src, dst, ignore = ignore_pyc_files)

chevron_right


Since ignoring filename patterns is common, a utility function ignore_patterns() has already been provided to do it as shown in the code given below.

Code #4 :

filter_none

edit
close

play_arrow

link
brightness_4
code

shutil.copytree(src, dst, ignore = shutil.ignore_patterns('*~', '*.pyc'))

chevron_right


How it works?

  • Using shutil to copy files and directories is mostly straightforward.
  • However, one caution concerning file metadata is that functions such as copy2() only make the best effort in preserving this data.
  • Basic information, such as access times, creation times, and permissions, will always be preserved, but the preservation of owners, ACLs, resource forks, and other extended file metadata may or may not work depending on the underlying operating system and the user’s own access permissions.
  • The user probably wouldn’t want to use a function like shutil.copytree() to perform system backups.

When working with filenames, make sure to use the functions in os.path for the greatest portability (especially if working with both Unix and Windows).

Code #5 : Example

filter_none

edit
close

play_arrow

link
brightness_4
code

filename = '/Users/gfg/programs/abc.py'
  
import os.path
os.path.basename(filename)

chevron_right


'abc.py'

 

filter_none

edit
close

play_arrow

link
brightness_4
code

os.path.dirname(filename)

chevron_right


'/Users/gfg/programs'

 

filter_none

edit
close

play_arrow

link
brightness_4
code

os.path.split(filename)

chevron_right


('/Users/gfg/programs', 'abc.py')

 

filter_none

edit
close

play_arrow

link
brightness_4
code

os.path.join('/new/dir', os.path.basename(filename))

chevron_right


'/new/dir/spam.py'

 

filter_none

edit
close

play_arrow

link
brightness_4
code

os.path.expanduser('~/gfg/programs/spam.py')

chevron_right


'/Users/gfg/programs/abc.py'

One tricky bit about copying directories with copytree() is the handling of errors. For example, in the process of copying, the function might encounter broken symbolic links, files that can’t be accessed due to permission problems, and so on.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.