Questions tagged [copytree]

Use this tag for questions relevant to the `copytree()` function, from the shutil module of Python.

Quoting the ref:

shutil.copytree(src, dst, symlinks=False, ignore=None)

Recursively copy an entire directory tree rooted at src. The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories. Permissions and times of directories are copied with copystat(), individual files are copied using shutil.copy2().

is used for questions that are using the shutil module of Python, and have to do with the act of copying an entire directory, with or without manipulating it (e.g. filtering it).

48 questions
363
votes
15 answers

How do I copy an entire directory of files into an existing directory using Python?

Run the following code from a directory that contains a directory named bar (containing one or more files) and a directory named baz (also containing one or more files). Make sure there is not a directory named foo. import…
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
129
votes
4 answers

Copy directory contents into a directory with python

I have a directory /a/b/c that has files and subdirectories. I need to copy the /a/b/c/* in the /x/y/z directory. What python methods can I use? I tried shutil.copytree("a/b/c", "/x/y/z"), but python tries to create /x/y/z and raises an error…
prosseek
  • 182,215
  • 215
  • 566
  • 871
29
votes
1 answer

Python shutil copytree: use ignore function to keep specific files types

I'm trying to figure out how to copy CAD drawings (".dwg", ".dxf) from a source directory with subfolders to a destination directory and maintaining the original directory and subfolders structure. Original Directory:…
Peter Wilson
  • 1,075
  • 3
  • 14
  • 24
20
votes
5 answers

Filter directory when using shutil.copytree?

Is there a way I can filter a directory by using the absolute path to it? shutil.copytree(directory, target_dir, ignore = shutil.ignore_patterns("/Full/Path/To/aDir/Common")) This doesn't seem to work when trying to…
Goles
  • 11,599
  • 22
  • 79
  • 140
6
votes
2 answers

Is there a way to interrupt shutil copytree operation in Python?

I'm fairly new to programming in general. I need to develop a program that can copy multiple directories at once and also take into account multiple file type exceptions. I came across the shutil module which offers the copytree and ignore_patterns…
3
votes
1 answer

Using shutil library to copy contents of a directory from src to dst, but not the directory itself

So I have a directory called /src, which has several contents: a.png, file.text, /sub_dir1, /sub_dir2 I want to copy these contents into destination directory /dst such that the inside of /dst looks like this (assuming /dst is previously…
user313
  • 681
  • 1
  • 8
  • 21
2
votes
2 answers

Use Python to automate copying files to their corresponding target folders(shutil.copytree doesn't work as intended)

I wanted to automate the process of copying files (in their target folders) to their corresponding source folders (which has the same folder structure as the source) located in a different directory on computer...I tried to use python's…
Penny
  • 1,218
  • 1
  • 13
  • 32
2
votes
1 answer

Confusion regarding common references in original tree and it's copy returned by COPY-TREE

Chapter 13. Beyond Lists: Other Uses for Cons Cells of Practical Common Lisp states that objects referenced in common by the copy of the tree ((1 2) (3 4) (5 6)) returned by COPY-TREE and the original tree itself are the numbers 5, 6, and the symbol…
Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
2
votes
1 answer

Sending list of patterns to copytree's ignore_patterns gives TypeError: unhashable type: 'list'

I am working on a python script that throws a TypeError: unhashable type: 'list' when I try and use a list of extensions as copytree's ignore_patterns. I'm unsure, 1. Why this won't work, and 2. How to go about fixing it. # allFileTypes is the list…
2
votes
2 answers

The Ignore callback for python shutil.copytree() does not accept full path

I'd like to specify full paths to ignorable files and directories when calling shutil.copytree(). Something like def my_ignore(dir, files): # return ["exclude.file"] # working return ["/full_path_to/exclude.file"] # Not…
kakyo
  • 10,460
  • 14
  • 76
  • 140
2
votes
3 answers

Problems with shutil.copytree

I want to copy folder from my local server on my computer, using function shutil.copytree, i using macOS, but today i have problem,python always show me the same message,"[error 1] operation not permitted",but yesterday mine script work without…
user1387805
  • 21
  • 1
  • 4
1
vote
0 answers

shutil.copytree unable to avoid FileExistsError Python 3.7

I know that with Python 3.8 has been introduced 'dirs_exist_ok=True' argument but I am constrained on Python 3.7 I have tried with no success shutil.copytree(homePath, dst, symlinks=False, ignore=None) Is there a way to avoid the exception with 1…
federico
  • 27
  • 1
  • 4
1
vote
0 answers

Retry a copy acitivity in python if a network error is given?

I'm currently trying to create a python script. One of the steps is that it shall create a copy of a folder, including subfolders, and checks beforehand if the folder already exists. If yes, then it should delete the folder first. Unfortunately, the…
AzUser1
  • 183
  • 1
  • 14
1
vote
0 answers

Copy specific files with copy tree (Python)

I have this script: import os, sys, shutil, glob from distutils.dir_util import copy_tree if not os.path.exists('Files'): os.makedirs('Files') source_dir = os.path.join(os.environ["HOMEPATH"], "Desktop") dest_dir =…
LuCash
  • 23
  • 4
1
vote
1 answer

copy_tree not copying sub directories

Using Python 3.8 on Windows 10 OS. The first time I run copy_tree it works perfectly, i.e. all files and sub-directories are copied over as expected. When I manually delete the newly copied directory from the destination and run copy_tree again…
Mimmo
  • 11
  • 2
1
2 3 4