我是一個閱讀python(3.0)字節的新手。這是我用過的第一種編程語言。我被困在了創建一個備份zip文件的簡單程序的地方(p.75)。我使用python 3.1運行Windows 7(64位)。在此之前,我安裝了GNUWin32 +源,並將C:\ Program Files(x86)\ GnuWin32 \ bin添加到我的Path環境變量中。這是程序:新手需要關於Python教程的幫助
#!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,看看是否能ATLEAST工作:
>>> zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b
SyntaxError: invalid syntax (<pyshell#17>, line 1)
既然這樣做不起作用,本教程說要閱讀GNUWin32手冊獲取更多幫助。我仔細看過它,還沒有看到任何可以幫助我的東西。要看到,如果壓縮功能工作我沒有幫助(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
Unfortuneatly我實在無法理解「幫助」呢。不過,我用zip函數玩了一下,看看它是如何工作的。
>>> zip (r'C:AB\a')
<zip object at 0x029CE8C8>
所以看來,zip函數的工作原理,但我想我沒有正確使用它。請幫助我,並記住我還沒有太多的編程經驗。如果你想查看教程,你可以在www.swaroopch.com/notes/Python找到它。
看起來你應該做的'拉鍊-qr C:\備份\ 20100106143030.zip C:\ AB \一個C:\ AB \ b'在終端提示符處,而不是python提示符。這有幫助嗎? – 2010-01-07 00:18:35