Monday, April 21, 2025

Backup One File with Python - Simple Backup Script

import os, shutil

os.chdir('c:\\Python38-64\\')

shutil.copy('copy-me.txt', 'c:\\EXTERNAL-ARCHIVE')

This code uses the os and shutil modules to copy a file called copy-me.txt from the current working directory (c:\\Python38-64\\) to a directory called EXTERNAL-ARCHIVE located at the root of the C: drive.

import os, shutil

os.chdir('c:\\Python38-64\\')

The first line of code imports the os and shutil modules.

The os.chdir() function is used to change the current working directory to c:\\Python38-64\\. This is done to ensure that the source file, copy-me.txt, is located in the correct directory before it is copied.

shutil.copy('copy-me.txt', 'c:\\EXTERNAL-ARCHIVE')

The shutil.copy() function is then called with two arguments: the source file (copy-me.txt) and the destination directory (c:\\EXTERNAL-ARCHIVE). The shutil module provides a way to copy files and directories in a platform-independent manner.

This code is useful for automating file backup or archiving tasks, where files need to be regularly copied or moved from one location to another.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...