How do I copy or move files and directories in Python?

In Python, you can copy or move files and directories using the built-in modules `shutil` and `os`. These modules provide functions to manipulate files and directories easily, making it favorable for file management tasks.

Keywords: Python, copy files, move files, shutil, os, file management
Description: Learn how to copy and move files and directories in Python using the shutil and os modules. This guide provides clear examples for better understanding.

import shutil
import os

# Copy a file
shutil.copy('source_file.txt', 'destination_file.txt')

# Move a file
shutil.move('source_file.txt', 'new_folder/source_file.txt')

# Copy a directory
shutil.copytree('source_folder', 'destination_folder')

# Move a directory
shutil.move('source_folder', 'new_location/source_folder')
    

Keywords: Python copy files move files shutil os file management