-1

I'm a newbie programmer and I started reading A Byte of Python. The first problem in the book is a program that needs to solve the following problem: "Create a backup of all my important files". This is the design of the program:

The files and directories to be backed up are specified in a list.

The backup must be stored in a main backup directory.

The files are backed up into a zip file.

The name of the zip archive is the current date and time.

This is the implementation:

    import os

    import time

    source = ['/Users/swa/notes']

    target_dir = '/Users/swa/backup'

    target = target_dir + os.sep + \
             time.strftime('%Y%m%d%H%M%S') + '.zip'

    if not os.path.exists(target_dir):
        os.mkdir(target_dir)

    zip_command = "zip -r {0} {1}".format(target,
                                          ' '.join(source))

    print "Zip command is:"

    print zip_command

    print "Running:"

    if os.system(zip_command) == 0:
        print 'Successful backup to', target
    else:
        print 'Backup FAILED'

I don't understand the

    target = target_dir + os.sep + \
             time.strftime('%Y%m%d%H%M%S') + '.zip'
    if not os.path.exists(target_dir):
        os.mkdir(target_dir)
    zip_command = "zip -r {0} {1}".format(target,
                                          ' '.join(source)) 

part. I have searched python documentation to understand what os.sep and os.mkdir and time.strftime ... is but if someone could explain it all to me I would be very grateful

chilliefiber
  • 571
  • 2
  • 7
  • 18

1 Answers1

1

os.sep is a platform independent file separator string. Specifically, on windows it is '\' and on other operating systems it is '/'.

os.mkdir makes a directory at from the given path. It raises an exception if the file or directory already exists.

time.strftime('%Y%m%d%H%M%S') formats the time datetime into a string with format %Y substituted for the four digit year, %m is the month as an integer, %d is the day of the month, %H is the hour, %M is the minute, and %S is the seconds.

Andrew Johnson
  • 3,078
  • 1
  • 18
  • 24
  • 1
    Why do you use os.sep? I understand the rest except why is target_dir in brackets in the if not statement. – chilliefiber Aug 03 '14 at 17:50
  • If you want to run the same code on windows and any other operating system then you need to use `os.sep` or `os.path.join`. Otherwise your program will crash. – Andrew Johnson Aug 03 '14 at 17:59
  • 1
    Ok, but what does it mean that is a file separator string? Why do we nedd to separate files? Sorry for being so dumb – chilliefiber Aug 03 '14 at 18:04
  • The files on your computer are separated into separate files and directories. A file `path` is a representation of where your file is located on disk. A file separator indicates that a file exists inside of another directory. For example on windows the root directory may be "C:" and a file in that directory would be "C:\myfile.txt". That backslash character is what I mean by file separator. It literally separates the directory from the file name. – Andrew Johnson Aug 03 '14 at 18:15
  • 1
    Ok, I think I understand what it means, I just don't get how it all fits in together. We import two modules because we need to use them, then create the variable source that contains the list Users/swa/notes and then we create the variable target_dir pointing to the directory users/swa/backup. So, what is the purpose of writing target = target_dir + os.sep + \ time.strftime('%Y%m%d%H%M%S') + '.zip' ? Btw, thanks for being so patient. – chilliefiber Aug 03 '14 at 18:26
  • `'.zip'` is a __datatype__ called a string. It is basically a list of letters, numbers, or symbols. This string consists of four symbols: `'.'`, `'z'`, `'i'`, and `'p'`. Strings can be joined together by using the `+` operator. So `'a' + 'b'` results in `'ab'`. This long code snippet that you referenced creates a string representation of the file path for the file that we are going to create. We store this file path in the variable `target`. That equals sign indicates that the value to the right of it will be stored in the variable on the left. – Andrew Johnson Aug 03 '14 at 18:31