👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
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 shutil
shutil.copytree("bar", "foo")
shutil.copytree("baz", "foo")
It will fail with:
$ python copytree_test.py
Traceback (most recent call last):
File "copytree_test.py", line 5, in <module>
shutil.copytree("baz", "foo")
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/shutil.py", line 110, in copytree
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.py", line 172, in makedirs
OSError: [Errno 17] File exists: "foo"
I want this to work the same way as if I had typed:
$ mkdir foo
$ cp bar/* foo/
$ cp baz/* foo/
Do I need to use shutil.copy()
to copy each file in baz
into foo
? (After I"ve already copied the contents of "bar" into "foo" with shutil.copytree()
?) Or is there an easier/better way?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How do I copy an entire directory of files into an existing directory using Python?, check other ast Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How do I copy an entire directory of files into an existing directory using Python?
- Deutsch How do I copy an entire directory of files into an existing directory using Python?
- Français How do I copy an entire directory of files into an existing directory using Python?
- Español How do I copy an entire directory of files into an existing directory using Python?
- Türk How do I copy an entire directory of files into an existing directory using Python?
- Русский How do I copy an entire directory of files into an existing directory using Python?
- Português How do I copy an entire directory of files into an existing directory using Python?
- Polski How do I copy an entire directory of files into an existing directory using Python?
- Nederlandse How do I copy an entire directory of files into an existing directory using Python?
- 中文 How do I copy an entire directory of files into an existing directory using Python?
- 한국어 How do I copy an entire directory of files into an existing directory using Python?
- 日本語 How do I copy an entire directory of files into an existing directory using Python?
- हिन्दी How do I copy an entire directory of files into an existing directory using Python?
New York | 2023-02-06
Thanks for explaining! I was stuck with How do I copy an entire directory of files into an existing directory using Python? for some hours, finally got it done 🤗. Will get back tomorrow with feedback
Singapore | 2023-02-06
Maybe there are another answers? What How do I copy an entire directory of files into an existing directory using Python? exactly means?. Will use it in my bachelor thesis
Singapore | 2023-02-06
Maybe there are another answers? What How do I copy an entire directory of files into an existing directory using Python? exactly means?. Will get back tomorrow with feedback