Python file copy

Do you need to copy 100s of files? Python is great tool to automate this task. It has built-in module shutil with copy2() function that can copy file with file's metadata.

This notebook was created with MLJAR Studio

MLJAR Studio is Python code editior with interactive code recipes and local AI assistant.
You have code recipes UI displayed at the top of code cells.

Documentation

Let's used documents directory in this notebook:

documents/
├── best-moments.mp4
├── music
│   ├── bass-guitar-sample.mp3
│   ├── first-song.mp3
│   ├── italo-disco.mp3
│   └── rave.wav
├── my-cv.pdf
├── phd-thesis.txt
├── todo.txt
└── video
    ├── holidays-2023.mp4
    ├── holidays-2024.mp4
    └── zoo.mov

We would like to copy best-moments.mp4 file to video directory.

# import packages
import shutil
# copy file
shutil.copy2(r"/home/piotr/documents/best-moments.mp4", r"/home/piotr/documents/video")
print("File copied")

The documents directory after file copy:

documents/
├── best-moments.mp4
├── music
│   ├── bass-guitar-sample.mp3
│   ├── first-song.mp3
│   ├── italo-disco.mp3
│   └── rave.wav
├── my-cv.pdf
├── phd-thesis.txt
├── todo.txt
└── video
    ├── best-moments.mp4
    ├── holidays-2023.mp4
    ├── holidays-2024.mp4
    └── zoo.mov

We have best-moments.mp4 file in documents and documents/video directories. File was copied with Python.

Recipes used in the python-copy-file.ipynb

All code recipes used in this notebook are listed below. You can click them to check their documentation.

Packages used in the python-copy-file.ipynb

List of packages that need to be installed in your Python environment to run this notebook. Please note that MLJAR Studio automatically installs and imports required modules for you.