我是 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)备份失败