【问题标题】:create a .zip file for backup in python 3 with os x使用 os x 在 python 3 中创建一个用于备份的 .zip 文件
【发布时间】:2013-01-16 01:51:20
【问题描述】:

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

存在。我在 macbook pro 上使用 python 3。我正在使用一个名为“komodo edit”的编辑器 我使用 CH Swaroop 的“Byte of 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) 备份失败

【问题讨论】:

    标签: macos zip backup komodo python-3.3


    【解决方案1】:

    您正在查看的示例代码完全是 Windows 特定的,C:\blah 路径是什么。 OS X 上的等效路径如下所示:

    /Users/yourusername/Documents
    

    (在转换代码时,您似乎还以某种方式将一些's 转换为,s。不知道这是怎么回事。)

    【讨论】:

    • 谢谢,我不知道我使用的是特定于 Windows 的。当我从书中复制和粘贴时,我忘记将 , 更改为 '。有效。但这复制了目录和该目录和子目录中的所有文件。你知道为什么这本书使用 [] 作为来源吗?在玩过代码之后,好像我什至不需要'/scan'。假设 [] 中的第二个列表是文件名,我错了吗?我将如何访问和写入外部硬盘?再次非常感谢。
    • 请忽略“复制的目录和所有......”这本书解释说 -r 负责子目录。
    【解决方案2】:

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

    为了诊断问题,我对您的代码进行了如下修改(将一些逗号更改为撇号,更改 strftime() 调用并添加 print() 以显示 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:\learningPythonc:\scan 并将文件放入 c:\learningPython 以便 zip 有事可做时,修改后的脚本在我的 Windows PC 上给出了以下输出:

    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
    

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

    我发现当我从 Python 运行系统命令时,将命令打印出来总是值得的,就像我要求 os.system() 运行它一样,以便我确切知道将要运行什么(并且因此,如果它没有执行我认为应该执行的操作,我可以从 shell 手动尝试该命令)。

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

    关于您的其他问题,source 是要求zip 处理的目录列表。如果你只需要处理一个目录,你可以很容易地将zip_command开头的行写成(这次是OS X / Unix风格的文件夹名称):

    zip_command = "zip -qr {0} {1}".format(target, '/Users/yourusername/Documents'))
    

    【讨论】:

    • 也谢谢你。我会更加小心我的大写字母和小写字母。
    猜你喜欢
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多