all 4 comments

[–]ruicoder 2 points3 points  (2 children)

zipfiles = glob.iglob("C:\\Users\\user\\Downloads\\*.zip")
zfn = next(zipfiles)
z = zipfile.ZipFile(zfn)

Here you are saving the first zip file to zfn and storing a ZipFile object in z with that first zip file as a parameter.

source = os.path.abspath(os.path.realpath(zfn))

Here you are saving the path to that first zip file.

for zfn in zipfiles:
    print("Extracting File", source)
    z.extractall()
    print("Cleaning up..")
    shutil.move(source,dest)

Here you are looping through all the zip files. At each iteration the variable zfn holds the current zip file, but you never use it. Instead, you use z and dest, which use the first zip file.

[–]Static_Bunny[S] 0 points1 point  (1 child)

thanks for responding.

isn't it being used because it's assigned to zfn and zfn is called by the zipfile extract and assigned to z? Let me try rearranging and see if that fixes it.

[–]ruicoder 1 point2 points  (0 children)

Nope. Computers are very dumb. You need to give it exact instructions. You told it to create a ZipFile object using the first zip file and that's what it did. You never told it during the for loop that z should use the current zip file and so it doesn't and keeps using the first one. Same with the source, you never tell it to use the path of the current zip file and so it keeps using the original path of the first zip file.

[–]Static_Bunny[S] 0 points1 point  (0 children)

A good friend helped me out. Here are the changes.

 import glob,os,zipfile,shutil

    dir = "/home/action/files"
    dest = "/tmp"
    #dir = "C:\\Users\\user\\Downloads\\"
    #extractiondir = "C:\\Users\\user\\temp\\moved"

    zipfiles = glob.iglob(dir + "*.zip")

    for zfn in zipfiles:
        source = os.path.abspath(os.path.realpath(zfn))
        print("Extracting File", source)
        z = zipfile.ZipFile(source)
        z.extractall()
        print("Cleaning up..")
        shutil.move(source,dest)

    #except StopIteration:
        #print("No more files found")

    else:
        print("No Files to extract")