I want a program which creates a backup of all my important files.
zipcommand 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 the
zipcommand from the GnuWin32 project page and add
C:\Program Files\GnuWin32\binto your system
PATHenvironment variable, similar to what we did for recognizing the python command itself.
Zip command isline 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.
timemodules by first importing them. Then, we specify the files and directories to be backed up in the
sourcelist. The target directory is where we store all the backup files and this is specified in the
target_dirvariable. 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
.zipextension and will be stored in the
os.sepvariable - 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.sepinstead 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
%Yspecification will be replaced by the year with the century. The
%mspecification will be replaced by the month as a decimal number between
12and so on. The complete list of such specifications can be found in the Python Reference Manual.
zip_commandwhich 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).
zipcommand that we are using has some options available, and one of these options is
-roption 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
sourcelist into a string using the
joinmethod of strings which we have already seen how to use.
os.systemfunction which runs the command as if it was run from the system i.e. in the shell - it returns
0if 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
r'C:\Documents'. However, do not use
'C:\Documents'since you end up using an unknown escape sequence
os.path.existsfunction. If it doesn't exist, we create it using the
+) 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.
inputfunction and then check if the user actually entered something by finding out the length of the input using the
lenfunction. If the user has just pressed
enterwithout entering anything (maybe it was just a routine backup or no special changes were made), then we proceed as we have done before.
.zipextension. Notice that we are replacing spaces in the comment with underscores - this is because managing filenames without spaces is much easier.
-voption to make your program become more talkative or a
-qoption to make it quiet.
sys.argvlist and we can add them to our
sourcelist using the
extendmethod provided by the
os.systemway 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.systemway 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.