Python 3:正确格式化zip模块参数(newb)

时间:2022-03-15 03:55:53

Please tell me why this code fails. I am new and I don't understand why my formatting of my zip arguments is incorrect. Since I am unsure how to communicate best so I will show the code, the error message, and what I believe is happening.

请告诉我为什么这段代码失败了。我是新手,我不明白为什么我的zip参数格式不正确。由于我不确定如何进行最佳沟通,因此我将展示代码,错误消息以及我认为正在发生的事情。

#!c:\python30
# Filename: backup_ver5.py

import os
import time
import zipfile


source = r'"C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list"'

target_dir = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir'

today = target_dir + os.sep + time.strftime('%Y%m%d') 

now = time.strftime('%H%M%S')

comment = input('Enter a comment --> ')

if len(comment) == 0:
    target = '"' + today + os.sep + now + '.zip' + '"'
else:
    target = '"' + today + os.sep + now + '_' + \
    comment.replace(' ', '_') + '.zip' + '"'

if not os.path.exists(today):
    os.mkdir(today)
    print('Successfully created directory', today)


print(target)
print(source)
zip_command = zipfile.ZipFile(target, 'w').write(source)

if os.system(zip_command) == 0:
    print('Successful backup to', target)
else:
    print('Backup FAILED')

    enter code here

I recieve this error message:

我收到此错误消息:

Enter a comment -->
"C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir\20090
405\134614.zip"
"C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list"
Traceback (most recent call last):
  File "C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_ve
r5.py", line 32, in <module>
    zip_command = zipfile.ZipFile(target, 'w').write(source)
  File "c:\python30\lib\zipfile.py", line 683, in __init__
    self.fp = io.open(file, modeDict[mode])
  File "C:\Python30\lib\io.py", line 222, in open
    closefd)
  File "C:\Python30\lib\io.py", line 615, in __init__
    _fileio._FileIO.__init__(self, name, mode, closefd)
IOError: [Errno 22] Invalid argument: '"C:\\Documents and Settings\\Benjamin Ser
rato\\My Documents\\python\\backup_dir\\20090405\\134614.zip"'

The two print tests before zip_command is assigned tell me that the two strings are being passed to zipfile.ZipFile() correctly. The traceback tells me I am not calling zipfile.ZipFile() correctly. The error in __init__ makes me more sure of this. Last, the problem seems to be that I am causing my path string to have double backslashes. I can't follow why the IOError shows that.

分配zip_command之前的两个打印测试告诉我两个字符串正确地传递给zipfile.ZipFile()。回溯告诉我,我没有正确调用zipfile.ZipFile()。 __init__中的错误让我对此更加肯定。最后,问题似乎是我导致我的路径字符串有双反斜杠。我无法理解为什么IOError会显示出来。

I used this site to figure out how to use zipfile. zipfile is a class, I import it at the start of the program then I use it and its primary method. I pass the file I would like to write to zipfile.ZipFile('file to write', 'mode') and set the program to open an object set to be writable. Then the command writes the file to the destination folder with a sub-method like so, "".zipfile('files to write').

我用这个网站来弄清楚如何使用zipfile。 zipfile是一个类,我在程序开始时导入它,然后我使用它和它的主要方法。我将要写入的文件传递给zipfile.ZipFile('file to write','mode')并将程序设置为打开一个可写的对象。然后,该命令使用类似的子方法将文件写入目标文件夹,“”。zipfile('要写入的文件')。

Where am I going wrong?

我哪里错了?

2 个解决方案

#1


Looks like it's because you have an extra pair of double quotes around your pathname. Remove the double quotes, and see if it works.

看起来是因为你的路径名周围有一对额外的双引号。删除双引号,看看它是否有效。

#2


To answer your other question: the double backslashes are there because they are escaped.

回答你的另一个问题:双反斜杠是因为它们被转义了。

#1


Looks like it's because you have an extra pair of double quotes around your pathname. Remove the double quotes, and see if it works.

看起来是因为你的路径名周围有一对额外的双引号。删除双引号,看看它是否有效。

#2


To answer your other question: the double backslashes are there because they are escaped.

回答你的另一个问题:双反斜杠是因为它们被转义了。