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

Im receiving this error:

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

Here is my code: code

I am honestly clueless... Here is code in text in case you want to mess with it:

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()


Solution 1:[1]

First parameter of ZipFile.write() should be file name which exist one. Create temp file and delete it after writing. Also your while loop has a small mistake in it's logic, after typing 'quit' code runs for 'quit' file.

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1