使用os x在python 3中创建一个.zip文件进行备份

时间:2013-01-16 01:51:20

标签: macos zip backup komodo python-3.3

我是编程python的新手(现在是5天),遇到了一个问题,我似乎无法在这个论坛中找到答案。我将非常感谢任何帮助和非常感谢。

成为。我在macbook pro上使用python 3。我正在使用名为" komodo edit" 我正在使用CH Swaroop" Python的字节"作为指导。

我遇到的问题是创建一个程序,该程序从一个文件夹获取文件并将其作为zip文件备份到另一个文件夹。

书中的例子如下:

Save as backup_ver1.py:
import os
import time
#  The files and directories to be backed up are specified in a list.
source = ['"C:\\My Documents"', 'C:\\Code']
# Notice we had to use double quotes inside the string for names with spaces in it.
#  The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using
#  The files are backed up into a zip file.
#  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') + '.zip'
#  We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
    print('Successful backup to', target)
else:
    print('Backup FAILED')

我的代码如下

import os
import time
source = ['C:\\learningPython','C:\\scan'] # can i use / instead of the \?
# why is square brackets used here? i'm under the assumption the square brackets are used for list. i'm also assuming one is the directory and the other is the file.
target_dir = 'C:/backup' # created a "backup" folder
target = target_dir + os.sep +time.strftime('%Y%M%D%H%M%S') + '.zip'
zip_command = "zip -qr {0} {1}".format(target, ‚ ‚.join(source))
if os.system(zip_command) == 0:
    print(‚Successful backup to‚, target)
else:
    print(‚Backup FAILED‚)

我得到了

zip错误:无所事事! (尝试:zip -qr C:/backup/20133201/15/13203219.zip.-i C:learningPythonC:scan) 备份失败

2 个答案:

答案 0 :(得分:0)

您正在查看的示例代码完全取决于Windows,使用C:\blah路径。 OS X上的等效路径类似于:

/Users/yourusername/Documents

(在转换代码时,您似乎也以某种方式将'转换为,。不确定这是什么。)

答案 1 :(得分:0)

正如另一位回答者所指出的那样,您使用的代码是特定于Windows的,并且需要其他答案建议的修改才能使其在OSX中运行。

要诊断问题,我已对代码进行了一些修改,如下所示(将逗号更改为撇号,更改strftime()调用并添加print()以显示{{1}中的实际内容}}):

zip_command

当我还创建了三个文件夹import os import time source = ['C:\\learningPython','C:\\scan'] target_dir = 'C:/backup' target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip' # modified zip_command = "zip -qr {0} {1}".format(target, ' '.join(source)) print(zip_command) # added - see discussion below if os.system(zip_command) == 0: print('Successful backup to', target) else: print('Backup FAILED') c:\backupc:\learningPython并将文件放在c:\scan中以便c:\learningPython有事可做时,修改后的脚本在我的Windows PC上提供了以下输出:

zip

......我想,这就是你所希望的。

我发现当我从Python运行系统命令时,总是需要打印命令,就像我要求C:\usr\sjl\dev\test\python>python ziptest.py zip -qr C:/backup\20130116152602.zip C:\learningPython C:\scan Successful backup to C:/backup\20130116152602.zip 运行命令一样,这样我就知道将要运行什么(所以我可以手动从shell中尝试该命令,如果它没有做我认为应该做的事情)。

我还修改了os.system()中的格式字符串,因为原始版本在'Y'之后有一个大写'M'(它会显示分钟数而不是月数)和a资本'D'而不是天数的'd'。 “D”不是strftime()格式字符串中的有效代码字母(请参阅here)。对我来说,使用Python 3.2时,错误的字母在我尝试运行脚本时导致运行时错误。

关于您的其他问题,strftime()是要求source处理的目录列表。如果您只需要处理一个目录,就可以轻松地编写以zip开头的行(这次是OS X / Unix样式的文件夹名称):

zip_command