2010-01-07 62 views
2

我是一個閱讀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找到它。

+1

看起來你應該做的'拉鍊-qr C:\備份\ 20100106143030.zip C:\ AB \一個C:\ AB \ b'在終端提示符處,而不是python提示符。這有幫助嗎? – 2010-01-07 00:18:35

回答

1

提示「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是否存在?你應該將那個長的「zip」行復制/粘貼到命令提示符(點擊windows key + R,然後輸入「cmd」並回車),看看它是否有效;不是Python shell。

+0

是,存在C:AB \ a和C:\ AB \ b。他們是我創建的小文件夾。 好的,所以在Python中的zip函數與我應該在這裏使用的zip分開。在那種情況下,我需要做些什麼才能讓操作系統使用zip命令?如果有幫助,我安裝了7zip&GNUWin32。 – 2010-01-07 00:26:42

+0

'os.system(zip_command)'應該可以工作,就像你在代碼中一樣。在Windows命令提示符下(編輯指令到我的文章)嘗試該命令(zip -qr C:...等)。 – Sapph 2010-01-07 00:28:53

+0

確定在Windows命令提示符下嘗試過,沒有工作錯誤信息:'zip'不被識別爲內部或外部命令可操作的程序或批處理文件。所以現在怎麼辦? – 2010-01-07 00:44:34

1

這並不能爲shell命令zip提供幫助,它爲python函數zip提供了幫助,它與zip文件壓縮無關。

1

幾個建議供您使用,以提高:

使用subprocess.call([command', 'arg', 'arg', 'arg'])命令調用外部zip命令。這將確保正確調用該命令並正確轉義數據。使用os.path.join將路徑組件一起幹淨地連接起來。這也有助於防止細微的錯誤。

確保您的zip命令在命令提示符下工作,然後在python中進行嘗試。

-

除此之外,它看起來不錯。

PS。 zip()是內建的,用於連接兩個迭代器。不適用於壓縮文件。


重要提示:看安德魯的評論(複製在這裏供參考):
看起來你應該在終端提示符,而不是蟒蛇迅速做了zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b。這有幫助嗎? - 安德魯·麥格雷戈3分鐘前

+0

我試圖將其複製到命令提示符,似乎操作系統無法識別命令'zip'由於某種原因。我不熟悉subprocess.call,所以我不得不閱讀如何使用它來調用zip命令。 – 2010-01-07 00:58:40

2
>>> zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b 

聽起來像是你應該輸入您的操作系統的shell,而不是在蟒蛇的shell命令。也許你可以試試

os.system('zip -qr C:\Backup\20100106143030.zip C:\AB\a C:\AB\b') 
在Python殼

...

+0

複製粘貼到shell輸出是1,所以我猜這意味着它的工作?雖然我沒有看到備份文件。 – 2010-01-07 00:34:29