【发布时间】:2010-11-24 20:07:02
【问题描述】:
我是编程新手。我正在尝试使用 CH Swaroop 的“Byte of 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 的命令行中为我设计一种方法吗?感谢您的宝贵时间,如果我未能提供一些相关信息,我提前道歉:)
【问题讨论】:
-
多么愚蠢的练习。 Python有
zipfile,他求助于调用外部程序?最重要的是,它正在教授os.system(),这几乎已经过时了。浪费时间。 -
但如果你不管...info-zip.org
-
自 2000 年以来,Python 使用
zipfile模块已有 年。“Python 字节”比这要新得多。 docs.python.org/library/zipfile.html -
如果你想在命令行上做一些事情,你需要查找你正在使用的 zip 程序的文档。它将解释如何做到这一点。如果你真的想压缩文件,你应该使用 python zip 模块。
-
MS DOS?我猜你的意思是 Windows。