I want a program which creates a backup of all my important files.
zip
command available by default in any standard GNU/Linux or Unix distribution. Note that you can use any archiving command you want as long as it has a command line interface.For Windows usersWindows users can install thezip
command from the GnuWin32 project page and addC:\Program Files\GnuWin32\bin
to your systemPATH
environment variable, similar to what we did for recognizing the python command itself.
backup_ver1.py
:Zip command is
line in the output, paste it in the shell (on GNU/Linux and Mac OS X) / cmd
(on Windows), see what the error is and try to fix it. Also check the zip command manual on what could be wrong. If this command succeeds, then the problem might be in the Python program itself, so check if it exactly matches the program written above.os
and time
modules by first importing them. Then, we specify the files and directories to be backed up in the source
list. The target directory is where we store all the backup files and this is specified in the target_dir
variable. The name of the zip archive that we are going to create is the current date and time which we generate using the time.strftime()
function. It will also have the .zip
extension and will be stored in the target_dir
directory.os.sep
variable - this gives the directory separator according to your operating system, i.e. it will be '/'
in GNU/Linux, Unix, macOS, and will be '\\'
in Windows. Using os.sep
instead of these characters directly will make our program portable and work across all of these systems.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 with the century. The %m
specification will be replaced by the month as a decimal number between 01
and 12
and so on. The complete list of such specifications can be found in the Python Reference Manual.zip_command
which contains the command that we are going to execute. You can check if this command works by running it in the shell (GNU/Linux terminal or DOS prompt).zip
command that we are using has some options available, and one of these options is -r
. The -r
option specifies that the zip command should work recursively for directories, i.e. it should include all the subdirectories and files. Options are followed by the name of the zip archive to create, followed by the list of files and directories to backup. We convert the source
list into a string using the join
method of strings which we have already seen how to use.os.system
function which runs the command as if it was run from the system i.e. in the shell - it returns 0
if the command was successfully, else it returns an error number.Note to Windows UsersInstead of double backslash escape sequences, you can also use raw strings. For example, use'C:\\Documents'
orr'C:\Documents'
. However, do not use'C:\Documents'
since you end up using an unknown escape sequence\D
.
backup_ver2.py
:os.path.exists
function. If it doesn't exist, we create it using the os.mkdir
function.backup_ver3.py
:+
) without any operand in that logical line and hence it doesn't know how to continue. Remember that we can specify that the logical line continues in the next physical line by the use of a backslash at the end of the physical line. So, we make this correction to our program. This correction of the program when we find errors is called bug fixing.backup_ver4.py
:input
function and then check if the user actually entered something by finding out the length of the input using the len
function. If the user has just pressed enter
without entering anything (maybe it was just a routine backup or no special changes were made), then we proceed as we have done before..zip
extension. Notice that we are replacing spaces in the comment with underscores - this is because managing filenames without spaces is much easier.-v
option to make your program become more talkative or a -q
option to make it quiet.sys.argv
list and we can add them to our source
list using the extend
method provided by the list
class.os.system
way of creating archives and instead using the zipfile or tarfile built-in modules to create these archives. They are part of the standard library and available already for you to use without external dependencies on the zip program to be available on your computer.os.system
way of creating a backup in the above examples purely for pedagogical purposes, so that the example is simple enough to be understood by everybody but real enough to be useful.