Help regarding Python backup script. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help regarding Python backup script.

CleanCutCleanCut Posts: 1Member
I am a newbie and currently learning python. I use a tutorial called "A Byte Of Python" and there I encountered a little problem with an example in the tutorial. It seems as he who wrote this tutorial are running on Linux and I am running on Windows. This is the code that he wrote(Swaroop C H):

import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:Documents', r'D:Work'] or something like that

# 2. The backup must be stored in a main backup directory
target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 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'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

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

as you can see this is a backup script who copies the the targeted files into another directory and compress them to Zip Files. My dilemma is that I cant find the windows command for compressing Zip files like they
have in Linux/Unix, can anyone help me?

Comments

  • CyGuyCyGuy Posts: 312Member
    : I am a newbie and currently learning python. I use a tutorial called "A Byte Of Python" and there I encountered a little problem with an example in the tutorial. It seems as he who wrote this tutorial are running on Linux and I am running on Windows. This is the code that he wrote(Swaroop C H):
    :
    : import os
    : import time
    :
    : # 1. The files and directories to be backed up are specified in a list.
    : source = ['/home/swaroop/byte', '/home/swaroop/bin']
    : # If you are using Windows, use source = [r'C:Documents', r'D:Work'] or something like that
    :
    : # 2. The backup must be stored in a main backup directory
    : target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using
    :
    : # 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'
    :
    : # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
    : zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
    :
    : # Run the backup
    : if os.system(zip_command) == 0:
    : print 'Successful backup to', target
    : else:
    : print 'Backup FAILED'
    : ------------------------------------------------------------------------
    :
    : as you can see this is a backup script who copies the the targeted files into another directory and compress them to Zip Files. My dilemma is that I cant find the windows command for compressing Zip files like they
    : have in Linux/Unix, can anyone help me?
    :
    [blue]I am a newbie as well, but simple things like sharing script have made me explicitly aware of this delemna that arises from windows XP. The problem is the file system is different. NTFS has a different format, so changes made under XP will not stick to anything from any other file system... not even older windows. Sorry bit I can't be of any further assistance currently.[/blue]
  • infidelinfidel Posts: 2,900Member
    : I am a newbie and currently learning python. I use a tutorial called "A Byte Of Python" and there I encountered a little problem with an example in the tutorial. It seems as he who wrote this tutorial are running on Linux and I am running on Windows. This is the code that he wrote(Swaroop C H):
    :
    : import os
    : import time
    :
    : # 1. The files and directories to be backed up are specified in a list.
    : source = ['/home/swaroop/byte', '/home/swaroop/bin']
    : # If you are using Windows, use source = [r'C:Documents', r'D:Work'] or something like that
    :
    : # 2. The backup must be stored in a main backup directory
    : target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using
    :
    : # 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'
    :
    : # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
    : zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
    :
    : # Run the backup
    : if os.system(zip_command) == 0:
    : print 'Successful backup to', target
    : else:
    : print 'Backup FAILED'
    : ------------------------------------------------------------------------
    :
    : as you can see this is a backup script who copies the the targeted files into another directory and compress them to Zip Files. My dilemma is that I cant find the windows command for compressing Zip files like they
    : have in Linux/Unix, can anyone help me?

    You may have to download a zip utility. There is a project at SourceForge, I think, called UnixUtils or something like that, which provides dozens of these command line utilities from Linux for your Windows machine.

    Python also comes with some libraries for compression. Look in your python documentation for the modules: zipfile, zlib, gzip, and bz2. There may even be others that I'm not aware of. Basically, the point is that you should be able to compress your files directly from python without having to call an external zip command.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.