0

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!")
5
  • Where do you create the path? Commented Dec 15, 2016 at 13:38
  • Are you sure this is the code you're running? At the moment the only line that can throw that error is the shutil.rmtree line (the rest are in the try and the error would be hidden) and that is using a different path (has .zip on the end). Commented Dec 15, 2016 at 13:40
  • Rather than messing around with r'' or '\\', you can use the fact Windows can handle '/' as a path separator. This will remove one possible problem from the equation. Commented Dec 15, 2016 at 13:40
  • sorry old code from an attempt to fix will edit now Commented Dec 15, 2016 at 13:40
  • @JohnColeman that didnt work mate Commented Dec 15, 2016 at 13:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

the path is there, i am looking right at the zipfile
I dont belive you :) Go to the directory using the command-promt, do a dir and post your result.
Are you sure that the directory C:\Users\severna\Desktop\Non_Test_Python_Files\app4export exists? (And i am NOT talking about the file C:\Users\severna\Desktop\Non_Test_Python_Files\app4export.zip)
want me to copy and paste the whole thing?
You DO NOT have a directory/folder named app4export. You only have a FILE called app4export.zip. Therefore you can not remove the DIRECTORY name app4export, because it does not exist.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.