user7297278
user7297278

Reputation:

why does python say it cant find the path specified when it made the path?

I made this program yesterday because I am using py2exe, so what this program does is it zips up the folder created by py2exe and names it to app4export so I can send it to my friends. I also added in where if i already have a zip file called app4export then it deletes it before hand, it worked yesterday but now today I get the error

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\severna\\Desktop\\Non_Test_Python_Files\\app4export'

but python made this location so I dont get why it cant find it later?

import os
import zipfile
import shutil

def zip(src, dst):
    zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print('zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname))
            zf.write(absname, arcname)
    zf.close()

source=r"C:\Users\severna\Desktop\Non_Test_Python_Files\dist"
destination=r"C:\Users\severna\Desktop\Non_Test_Python_Files\app4export"

shutil.rmtree(str(destination))

try:
    zip(str(source), str(destination))
    shutil.rmtree(str(source))

except FileNotFoundError:
    print("Source cannot be zipped as it does not exist!")

Upvotes: 0

Views: 2006

Answers (2)

user7297278
user7297278

Reputation:

after discussion with Cleared I found out that I needed i file extension because it was a file and shutil.rmtree doesnt remove files it removes directories so I need to use this code instead

os.remove(str(destination)+".zip")

Upvotes: 0

Cleared
Cleared

Reputation: 2580

Your code creates the file C:\Users\severna\Desktop\Non_Test_Python_Files\app4export.zip, but you try to remove the directory C:\Users\severna\Desktop\Non_Test_Python_Files\app4export

So just before the try-block you have

shutil.rmtree(str(destination))

which will throw an FileNotFoundError if the path do not exist. And when you hit that line of code, you still havent created the path. The reason it might have worked yesterday was that you mayby had that path.

Upvotes: 1

Related Questions