我是一个新手,正在使用python (3.0)的一个字节。这是我用过的第一种编程语言。我被困在你做一个简单的程序来创建一个备份压缩文件(p.75)的地方。我使用的是Windows7 (64位)和python 3.1。在此之前,我安装了GNUWin32 +源代码,并将C:\Program Files(x86)\GnuWin32\bin添加到我的Path环境变量中。程序是这样的:
#!C:\Python31\mystuff
# Filename : my_backup_v1.py
import os
import time
# backing up a couple small files that I made
source = [r'C:\AB\a', r'C:\AB\b']
#my back up directory
target_dir = 'C:\\Backup'
#name of back up file
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = "zip -qr {0} {1}".format(target,' '.join(source))
print(zip_command)
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup failed!')
print('source files are', source)
print('target directory is', target_dir)
print('target is', target)
输出:
zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
Backup failed!
source files are ['C:\\AB\\a', 'C:\\AB\\b']
target directory is C:\Backup
target is C:\Backup\20100106143030.zip
本教程包括一些故障排除建议:在python shell提示符中复制并粘贴zip_command,看看是否有效:
>>> zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
SyntaxError: invalid syntax (<pyshell#17>, line 1)
由于这不起作用,教程建议阅读GNUWin32手册以获得更多帮助。我已经看过了,但还没有看到任何对我有帮助的东西。为了查看zip函数是否正常工作,我提供了帮助(Zip),并获得了以下内容:
>>> help(zip)
Help on class zip in module builtins:
class zip(object)
| zip(iter1 [,iter2 [...]]) --> zip object
|
| Return a zip object whose .__next__() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The .__next__()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __next__(...)
| x.__next__() <==> next(x)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object at 0x1E1B8D80>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
不幸的是,我还不能真正理解“帮助”。不过,我尝试了一下zip函数,看看它是如何工作的。
>>> zip (r'C:AB\a')
<zip object at 0x029CE8C8>
所以看起来zip函数是有效的,但我猜我没有正确使用它。请帮帮我,别忘了我还没有多少编程经验。如果你想看这个教程,你可以在www.swaroopch.com/note/Python找到它。
发布于 2010-01-07 08:20:06
"zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
“在提示符下失败的原因是,在这种情况下,"zip”应该是您发送给操作系统的命令……与Python无关。
我担心这有点令人困惑--当您使用"zip(r'C:AB\a')
“时,您使用的是Python自带的zip()
函数,该函数与您要做的事情无关。
你有合适的目录结构吗?我的意思是,C:\AB\a和C:\AB\b存在吗?
Python你应该把那个长的“”行复制/粘贴到命令提示符(点击windows键+ R,然后键入"cmd“并点击enter),看看它是否能工作;而不是Python shell。
发布于 2010-01-07 08:21:33
>>> zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
听起来像是应该在操作系统的shell中键入的命令,而不是python的shell中的命令。也许你可以试试
os.system('zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b')
在python shell中...
发布于 2010-01-07 08:18:17
这并不是为zip
命令提供帮助,而是为zip
函数提供帮助,该函数与压缩文件无关。
https://stackoverflow.com/questions/2017320
复制相似问题