Skip to content

valour02/File-Backup-Using-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Code Documentation

Overview

This Python script provides a utility to back up files from a source directory to a destination directory. The code uses the os module for file and directory operations and the shutil module for copying files. It ensures that all files in the source directory are copied to the destination directory, creating the destination directory if it does not exist.


Modules Used

  1. os:

    • Handles file and directory path operations.
    • Functions used:
      • os.path.exists(): Checks if a directory exists.
      • os.makedirs(): Creates a directory.
      • os.listdir(): Lists all files in the source directory.
      • os.path.isfile(): Checks if a path refers to a file.
  2. shutil:

    • Provides file and directory management operations.
    • Function used:
      • shutil.copy(): Copies a file from the source to the destination.

Code Breakdown

Function: backup_files(src_dir, dest_dir)

Purpose:

To back up all files from the source directory (src_dir) to the destination directory (dest_dir).

Steps:

  1. Check Destination Directory:
    • If the destination directory does not exist, create it using os.makedirs(dest_dir).
  2. Iterate Over Files in Source Directory:
    • Use os.listdir(src_dir) to get a list of all items in the source directory.
    • Combine the source directory path and the file name using os.path.join() to get the full file path.
    • Use os.path.isfile() to ensure only files (not directories) are processed.
  3. Copy Files:
    • Copy each file to the destination directory using shutil.copy().
    • Print a confirmation message for each file copied.

Variables:

  1. source: The path to the source directory containing files to back up.
    • Example: r"C:\Users\naveen kumar pandey\Desktop\PAPER_\TEST"
  2. destination: The path to the destination directory where files will be copied.
    • Example: r"C:\Users\naveen kumar pandey\Desktop\PAPER_\TRY"

Execution:

  1. The backup_files function is called with source and destination as arguments.
  2. Files from the source directory are backed up to the destination directory.

Example Output

For a file named example.txt in the source directory, the script will output:

Backed up example.txt to C:\Users\naveen kumar pandey\Desktop\PAPER_\TRY

Key Notes:

  1. File Overwriting:
    • If a file with the same name exists in the destination directory, it will be overwritten.
  2. Platform Compatibility:
    • Paths use raw string (r"") to handle Windows-style paths with backslashes (\).

Improvements:

  • Add error handling for scenarios such as:
    • Source directory does not exist.
    • Permission issues during file copy.
  • Log messages to a file instead of printing to the console.
  • Enhance to handle nested directories.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published