在MS DOS中用Python创建.zip文件
我刚开始学习编程,正在用CH Swaroop的《Python入门》来学习Python。其中有一个例子是教我们如何把一些文件从一个文件夹备份到另一个文件夹,并把它们压缩成.zip格式。不过,他给的例子主要是针对Linux或Unix用户的。对于Windows用户,他只提到“Windows用户可以使用Info-Zip程序”,但没有进一步解释。以下是他提供的代码……
#!/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 :('
有没有人能告诉我在Windows 7的命令行中怎么做这个?谢谢你们的帮助,如果我没有提供足够的信息,我提前道歉 :)
3 个回答
0
这段代码是用来展示一些编程概念的。它可能包含了一些函数、变量或者其他编程元素,目的是帮助大家理解代码是如何工作的。
在编程中,代码就像是给计算机下达的指令。每一行代码都有它的作用,组合在一起就能完成特定的任务。比如,可能有一行代码是用来计算数字的,另一行是用来显示结果的。
如果你看到一些看起来复杂的代码,不用担心,慢慢来,逐行理解它们的意思就好。编程就像学习一种新的语言,开始时可能会觉得难,但多练习就会变得越来越简单。
#!/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'
0
zip
是一个命令行工具,用来创建、更新或提取 ZIP 压缩文件,这个工具在 Unix/Linux/Mac OS X 系统上都可以使用。如果你想通过命令行来压缩文件,你需要找到并安装一个合适的工具(比如 compress
,它是资源工具包的一部分)。
另外一种方法是使用 Python 的 zipfile
模块,自己制作一个适合 Windows 的命令行工具 :)
顺便问一下,你的问题为什么提到了 MS DOS 呢?
1
对于使用Python 3的用户,如果你使用format方法,答案应该是:
# 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')