Python File Backup Script Example

This beginner-friendly example shows how to create a simple Python file backup script.

The goal is small and practical:

  • copy one file to a backup file
  • check that the original file exists first
  • print a clear message about what happened

This is a good first project if you are learning how Python works with files and paths.

Quick example

Use this as the fastest working version:

from pathlib import Path
import shutil

source = Path("notes.txt")
backup = Path("notes_backup.txt")

if source.exists():
    shutil.copy(source, backup)
    print("Backup created:", backup)
else:
    print("Source file not found")

This script copies one file and helps you understand the main backup idea before adding folders, timestamps, or multiple files.

What this script does

  • Copies one file to a new backup file
  • Checks whether the source file exists first
  • Uses built-in Python tools
  • Shows a practical beginner project

What the user needs before starting

Before you run the script, make sure you have:

  • Python installed and working
  • A sample file to back up, such as notes.txt
  • A basic understanding of file paths
  • Permission to read the source file and write the backup file

If you are unsure how paths work, see working with file paths in Python.

Main idea behind a backup script

A basic backup script follows a simple process:

  1. Choose the original file
  2. Choose where the backup should be saved
  3. Check that the original file exists
  4. Copy the file
  5. Print a clear success or error message

That is all this example does.

Simple backup script walkthrough

Here is the same script again:

from pathlib import Path
import shutil

source = Path("notes.txt")
backup = Path("notes_backup.txt")

if source.exists():
    shutil.copy(source, backup)
    print("Backup created:", backup)
else:
    print("Source file not found")

Step 1: import the tools

from pathlib import Path
import shutil
  • Path from pathlib makes file paths easier to read and work with
  • shutil includes tools for copying files

For beginners, pathlib is usually easier to understand than older string-based path handling.

Step 2: choose the source and backup paths

source = Path("notes.txt")
backup = Path("notes_backup.txt")
  • source is the file you want to back up
  • backup is the new file that will be created

In this example, both files are in the current folder.

If you want to check whether a file exists in more detail, see how to check if a file exists in Python.

Step 3: check that the source file exists

if source.exists():

This prevents the script from trying to copy a file that is not there.

If the file does not exist, the script prints:

print("Source file not found")

This is a simple way to avoid a FileNotFoundError.

Step 4: copy the file

shutil.copy(source, backup)

This creates the backup file.

  • If notes_backup.txt does not exist, Python creates it
  • If it already exists, it will usually be overwritten

Step 5: print a success message

print("Backup created:", backup)

This lets you know the copy worked.

Expected result

If notes.txt exists and the script runs successfully:

  • A new file appears at the backup path
  • The copied file has the same contents as the original
  • The terminal prints a success message

Example output:

Backup created: notes_backup.txt

Useful improvement: add a timestamp to the backup name

A basic backup script can overwrite the old backup each time it runs.

Adding a timestamp helps because it:

  • prevents old backups from being overwritten
  • makes each backup easier to identify
  • creates a more useful real backup system

Example:

from pathlib import Path
from datetime import datetime
import shutil

source = Path("notes.txt")

timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
backup = Path(f"notes_backup_{timestamp}.txt")

if source.exists():
    shutil.copy(source, backup)
    print("Backup created:", backup)
else:
    print("Source file not found")

If you run this script multiple times, each backup file gets a different name.

Useful improvement: back up to another folder

You may want to keep backups in a dedicated folder instead of the same place as the original file.

Example:

from pathlib import Path
import shutil

source = Path("notes.txt")
backup_folder = Path("backups")
backup = backup_folder / "notes_backup.txt"

if source.exists():
    backup_folder.mkdir(exist_ok=True)
    shutil.copy(source, backup)
    print("Backup created:", backup)
else:
    print("Source file not found")

What changed here?

backup_folder = Path("backups")
backup = backup_folder / "notes_backup.txt"

This builds a path inside a folder called backups.

backup_folder.mkdir(exist_ok=True)

This creates the folder if it does not already exist.

If you want to learn more about writing files and saving output, see how to write to a file in Python.

Common problems

Here are some common reasons the script may not work:

  • The source file path is wrong
  • The backup folder does not exist
  • The script tries to copy a folder instead of a file
  • The user does not have permission to access the file

Other common causes include:

  • Using the wrong file path
  • Running the script from a different folder than expected
  • Trying to save to a folder that does not exist
  • Using shutil.copy() on a directory path
  • Permission problems when reading or writing files

If your file path is wrong, Python may raise FileNotFoundError: No such file or directory.

If Python cannot read or write the file, see PermissionError: Permission denied.

Beginner debugging steps

If the script is not working, try these simple checks.

print(source)

This helps you confirm the file name or full path you are using.

Check whether Python can find the file

print(source.exists())

If this prints False, Python cannot find the file at that path.

from pathlib import Path
print(Path.cwd())

This shows the folder where the script is currently running.

This is important because "notes.txt" means "look for notes.txt in the current working directory."

print(backup)

This helps you confirm where Python is trying to save the copied file.

Keep the test simple

For your first test:

  • put the script and notes.txt in the same folder
  • use simple names like notes.txt and notes_backup.txt
  • read the full error message carefully

Next steps after this example

After you understand this one-file backup script, you can improve it by:

  • backing up multiple files in a loop
  • backing up every file in a folder
  • creating dated backup names automatically
  • handling errors with try-except

You may also want to learn how to read a file in Python if you are building larger file tools.

FAQ

What is the easiest way to back up a file in Python?

Use shutil.copy() with a source path and a backup path.

Does this script copy folders too?

No. This basic example is for a single file. Folder backups need a different approach, such as shutil.copytree().

How do I avoid overwriting an old backup?

Add a timestamp to the backup file name so each backup is unique.

Why does Python say the file does not exist?

The path may be wrong, or the script may be running from a different working directory.

Should I use os or pathlib for file paths?

For beginners, pathlib is often easier to read and use.

See also