使用 zipfile 时收到一个奇怪的错误,我不确定为什么。程序看起来不错

Receiving a weird error while using zipfile and I'm not sure why. Program seems fine

我收到此错误:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'file1.txt'

这是我的代码: code

老实说,我一无所知... 这是文本中的代码,以防您想弄乱它:

from zipfile import ZipFile

zipName = ZipFile(input('Enter zip file name: '), 'w')
fileName = ''

while fileName != 'quit':
    fileName = input('Enter file name to zip (enter quit to exit): ')
    zipName.write(fileName)

zipName.close()

x = input()

ZipFile.write()的第一个参数应该是存在的文件名。创建临时文件并在写入后将其删除。您的 while 循环在逻辑上也有一个小错误,在输入 'quit' 代码后运行 'quit' 文件。

from zipfile import ZipFile
import os

zipName = ZipFile(input('Enter zip file name: '), 'w')
fileName = ''

while True:
    fileName = input('Enter file name to zip (enter quit to exit): ')
    if fileName == "quit":
        break
    open(fileName, 'w+').close()
    zipName.write(fileName)
    os.remove(fileName)