Creating a .zip archive with Python in MS DOS
I am new to programming. I am trying to learn Python using CH Swaroop's "Byte of Python". One example is to create a program which will backup some files from one directory to another and compress them into a .zip format. Unfortunately the example he gives is only helpful if you happen to be a linux/unix user. For windows users he says only "Windows users can use the Info-Zip program" but doesn't elaborate further. This is the code he provides ...
#!/usr/bin/python
# Filename : backup_ver1.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'C:\Users\ClickityCluck\Documents']
# 2. The backup must be stored in a main backup directory
target_dir = r'C:\Backup'
# 3. Zip seems good
# 4. Let's make the name of the file the current date/time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ''.join(source))
# Run Backup
if os.system(zip_command) == 0:
print "Succesful backup to", target
else:
print 'BACKUP FAILED :('
Can anyone lay out for me a way to do this in the command line on windows 7? Thank you for your time and I apologize in advance if I have faile开发者_开发知识库d to provide some pertinent information :)
For python 3 users and using the format method answer should be:
# 1. The files and directories to be backed up are specified in a list.
source = [r'C\Users\MySourceDir']
# 2. The backup must be stored in a # main backup directory
target_dir = r'C:\Users\MyTargetDir'
# 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 + os.sep + time.strftime('%Y%m%d%H%M%S') + '.7z'
# Create target directory if it is not present
if not os.path.exists(target_dir):
os.mkdir(target_dir)
# 5. We use the zip command to put the files in a zip archive
zip_command = 'C:\\"Program Files"\\7-Zip\\7z a -t7z -r "{0}" "{1}"'.format(target,' '.join(source)) # be careful with spaces on each dir specification. Problems could arise if double-quotation marks aren't between them.
# Run the backup
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
zip
is command line utility to create/update/extract ZIP archives, available on Unix/Linux/Mac OS X. If you want to archive files using a command line utility, you should find and install an appropriate on (compress
, for example, is a part of resource kit).
The another way is to use python's zipfile
module and make a useful command-line utility for windows :)
BTW, why your question refers to MS DOS?
#!/usr/bin/python -tt
# Filename: backup_ver1.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['C:\\Documents\\working_projects\\', 'C:\\Documents\\hot_tips\\']
# 2. The back up must be stored in a main back directory
target_dir = 'C:\\temp\\backup\\'
# 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') + '.7z'
# 5. We use the zip command to put the files in a zip archive
#zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
zip_command = 'C:\\"Program Files"\\7-Zip\\7z a -t7z "%s" %s' % (target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
print 'Successful back up to', target
else:
print 'Backup FAILED'
精彩评论