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.
-
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.
-
shutil:- Provides file and directory management operations.
- Function used:
shutil.copy(): Copies a file from the source to the destination.
To back up all files from the source directory (src_dir) to the destination directory (dest_dir).
- Check Destination Directory:
- If the destination directory does not exist, create it using
os.makedirs(dest_dir).
- If the destination directory does not exist, create it using
- 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.
- Use
- Copy Files:
- Copy each file to the destination directory using
shutil.copy(). - Print a confirmation message for each file copied.
- Copy each file to the destination directory using
source: The path to the source directory containing files to back up.- Example:
r"C:\Users\naveen kumar pandey\Desktop\PAPER_\TEST"
- Example:
destination: The path to the destination directory where files will be copied.- Example:
r"C:\Users\naveen kumar pandey\Desktop\PAPER_\TRY"
- Example:
- The
backup_filesfunction is called withsourceanddestinationas arguments. - Files from the
sourcedirectory are backed up to thedestinationdirectory.
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
- File Overwriting:
- If a file with the same name exists in the destination directory, it will be overwritten.
- Platform Compatibility:
- Paths use raw string (
r"") to handle Windows-style paths with backslashes (\).
- Paths use raw string (
- 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.