

# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
Now, we are in the testing phase where we test that our program works properly. If it doesn't behave as expected, then we have to debug our program i.e. remove the bugs (errors) from the program.
We make use of the os andtime modules and so we import them. Then, we specify the files and directories to be backed up in thesource list. The target directory is where store all the backup files and this is specified in thetarget_dir variable. The name of the zip archive that we are going to create is the current date and time which we fetch using thetime.strftime() function. It will also have the .zip extension and will be stored in thetarget_dir directory.
The time.strftime() function takes a specification such as the one we have used in the above program. The%Y specification will be replaced by the year without the cetury. The%m specification will be replaced by the month as a decimal number between01 and12 and so on. The complete list of such specifications can be found in the [Python Reference Manual] that comes with your Python distribution. Notice that this is similar to (but not same as) the specification used inprint statement (using the% followed by tuple).
We create the name of the target zip file using the addition operator which concatenates the strings i.e. it joins the two strings together and returns a new one. Then, we create a stringzip_command which contains the command that we are going to execute. You can check if this command works by running it on the shell (Linux terminal or DOS prompt).
The zip command that we are using has some options and parameters passed. The-q option is used to indicate that the zip command should work quietly. The-r option specifies that the zip command should work recursively for directories i.e. it should include subdirectories and files within the subdirectories as well. The two options are combined and specified in a shorter way as-qr. The options are followed by the name of the zip archive to create followed by the list of files and directories to backup. We convert thesource list into a string using thejoin method of strings which we have already seen how to use.
Then, we finally run the command using theos.system function which runs the command as if it was run from the system i.e. in the shell - it returns0 if the command was successfully, else it returns an error number.
Depending on the outcome of the command, we print the appropriate message that the backup has failed or succeeded and that's it, we have created a script to take a backup of our important files!You can set the source list andtarget directory to any file and directory names but you have to be a little careful in Windows. The problem is that Windows uses the backslash (\) as the directory separator character but Python uses backslashes to represent escape sequences!
So, you have to represent a backslash itself using an escape sequence or you have to use raw strings. For example, use 'C:\\Documents' or r'C:\Documents' but do not use 'C:\Documents' - you are using an unknown escape sequence\D !
Now that we have a working backup script, we can use it whenever we want to take a backup of the files. Linux/Unix users are advised to use the executable method as discussed earlier so that they can run the backup script anytime anywhere. This is called the operation phase or the deployment phase of the software.
The above program works properly, but (usually) first programs do not work exactly as you expect. For example, there might be problems if you have not designed the program properly or if you have made a mistake in typing the code, etc. Appropriately, you will have to go back to the design phase or you will have to debug your program.