0

我是 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 -qr C:/backup/20133201/15/13203219.zip .-i C:learningPythonC:scan)备份失败

4

2 回答 2

0

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

/Users/yourusername/Documents

(在转换代码时,您似乎也以某种方式将几个's 转换为,s。不知道那是什么。)

于 2013-01-16T02:13:54.670 回答
0

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

为了诊断问题,我对您的代码进行了一些修改,如下所示(将一些逗号更改为撇号,更改strftime()调用并添加 aprint()以显示 中的实际内容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以便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()格式字符串中的有效代码字母(请参阅此处)。对我来说,使用 Python 3.2,当我尝试运行脚本时,错误的字母会导致运行时错误。

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

zip_command = "zip -qr {0} {1}".format(target, '/Users/yourusername/Documents'))
于 2013-01-16T02:48:43.103 回答