Python Forum

Full Version: Working with existing files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok.
I've spent alot of time reading the forum posts related to files & stackoverflow & medium & google and more! (you get the point - i'm stuck.)

I have a zip function that works great on a single zip file but if I have more than 1 file with the same name
then the function overwrites the file.
I've tried to use the os.walk, os.isfile, os.exsist, adding else condition and the last solution that you see here.
still, i'm getting only 1 new file (filename[2].zip)

Please help!

def dozip(duration, limit=50):
    print(f"Zipping in {duration} seconds...")
    time.sleep(duration)
    root_dir = f"{os.environ['appdata']}"
    zip_files = []
    i = 1
    z = 1
    file_name = f"{sysinfo.node}[{i}].zip"
    root_files = os.listdir(root_dir)
    zip_files.append(root_files)
    for file in zip_files:
        if file_name == file:
            i += 1
            zipper = ZipFile(f"c:\\Users\\{os.getlogin()}\\Videos\\"
                             f"{sysinfo.node}[{i+1}].zip", 'w')
            
            try:
                zipper.write(f"{os.environ['appdata']}" + f"{sysinfo.node}.txt")
                                                          
            except FileNotFoundError as e:
                print(f"[!]File not found:\n{e}")
                pass

            try:
                while z < limit:
                    zipper.write(f"{os.environ['appdata']}" + f"{sysinfo.node}[{z + 1}].png")
                                                              
                    z += 1
                    if z >= limit:
                        dozip(0.1)

            except FileNotFoundError as e:
                print(f"[!]File not found:\n{e}")
                pass

            zipper.close()
            zip_files.append(f"{sysinfo.node}[{i}].zip")
            print("[+]Zipped!")

            return zipper
        
Thank you!
OMG!
so close!!!

solution:
using the len(list) in the numbered file's name and appending the name to the list at the end of each cycle.


    if len(zip_files) <= 1:
        zipper = ZipFile(f"c:\\Users\\{os.getlogin()}\\Videos\\"
                         f"{sysinfo.node}[{i}].zip", 'w')
        
    while len(zip_files) > 1:
        zipper = ZipFile(f"c:\\Users\\{os.getlogin()}\\Videos\\"
                         f"{sysinfo.node}[{len(zip_files)}].zip", 'w')
What is the dozip() call on line 30 for? Could it happen continually?
the dozip is a function that takes the files in folder X and zipps them to folder Y.
I can call it whenever I choose to.
I'm trying to make everything re-usable.

def dozip(duration, limit):
    print(f"[i]Zipping in {duration} seconds...")
    time.sleep(duration)
    root_dir = f"{os.environ['appdata']}" + f"\\Microsoft\Windows\Templates\\"
    z = 1
    i = 1
    file_name = f"{sysinfo.node}[{i}].zip"
    root_files = os.listdir(root_dir)
    zip_files.extend(root_dir)
    while len(zip_files) > 0:
        zipper = ZipFile(f"c:\\Users\\{os.getlogin()}\\Videos\\"
                         f"{sysinfo.node}[{len(zip_files)}].zip", 'w') 

        try:
            zipper.write(f"{os.environ['appdata']}" + f"\\Microsoft\\Windows\\Templates\\"
                                                      f"{sysinfo.node}.txt", f"{sysinfo.node}.txt")
        except FileNotFoundError as e:
            print(f"[!]File not found:\n{e}")
            pass

        try:
            for z in range(limit):
                zipper.write(f"{os.environ['appdata']}" + 
                             f"\\Microsoft\\Windows\\Templates\\" 
                             f"{sysinfo.node}[{z + 1}].png", f"{sysinfo.node}[{z + 1}].png")
                z += 1
                if z >= limit:
                    continue

        except FileNotFoundError as e:
            print(f"[!]File not found:\n{e}")
            pass

        zipper.close()
        print("[+]Zipped!")
        zip_files.append(f"{sysinfo.node}[{len(zip_files) -1}].zip")

        return zipper
(Feb-09-2021, 11:54 AM)Gilush Wrote: [ -> ]the dozip is a function that takes the files in folder X and zipps them to folder Y.
I can call it whenever I choose to.
I'm trying to make everything re-usable.
Only 1 bug left to fix and its that skipping by 1 part.
for some reason I get the file names like that:

filename[1].zip
filename[3].zip
filename[5].zip
...
filename[9].zip
filename[11].zip

def dozip(duration, limit):
    print(f"[i]Zipping in {duration} seconds...")
    time.sleep(duration)
    root_dir = f"{os.environ['appdata']}" + f"\\Microsoft\Windows\Templates\\"
    z = 1
    i = 1
    file_name = f"{sysinfo.node}[{i}].zip"
    root_files = os.listdir(root_dir)
    zip_files.extend(root_dir)
    while len(zip_files) > 0:
        zipper = ZipFile(f"c:\\Users\\{os.getlogin()}\\Videos\\"
                         f"{sysinfo.node}[{len(zip_files)}].zip", 'w') 

        try:
            zipper.write(f"{os.environ['appdata']}" + f"\\Microsoft\\Windows\\Templates\\"
                                                      f"{sysinfo.node}.txt", f"{sysinfo.node}.txt")
        except FileNotFoundError as e:
            print(f"[!]File not found:\n{e}")
            pass

        try:
            for z in range(limit):
                zipper.write(f"{os.environ['appdata']}" + 
                             f"\\Microsoft\\Windows\\Templates\\" 
                             f"{sysinfo.node}[{z + 1}].png", f"{sysinfo.node}[{z + 1}].png")
                z += 1
                if z >= limit:
                    continue

        except FileNotFoundError as e:
            print(f"[!]File not found:\n{e}")
            pass

        zipper.close()
        print("[+]Zipped!")
        zip_files.append(f"{sysinfo.node}[{len(zip_files) -1}].zip")

        return zipper
(Feb-09-2021, 06:48 AM)bowlofred Wrote: [ -> ]What is the dozip() call on line 30 for? Could it happen continually?

My initial plan was to zip the files while gathering them so I set a limit var.
for example: 50
so now when the func runs it will zip every 50 files but I think i'll just let it go since I can't find a solution to that double skip.
Starting at line 22, what do you think the "continue" is doing? Why not use a "break" there?
Quote:
            for z in range(limit):
                zipper.write(f"{os.environ['appdata']}" + 
                             f"\\Microsoft\\Windows\\Templates\\" 
                             f"{sysinfo.node}[{z + 1}].png", f"{sysinfo.node}[{z + 1}].png")
                z += 1
                if z >= limit:
                    continue
(Feb-10-2021, 04:59 PM)nilamo Wrote: [ -> ]Starting at line 22, what do you think the "continue" is doing? Why not use a "break" there?
Quote:
            for z in range(limit):
                zipper.write(f"{os.environ['appdata']}" + 
                             f"\\Microsoft\\Windows\\Templates\\" 
                             f"{sysinfo.node}[{z + 1}].png", f"{sysinfo.node}[{z + 1}].png")
                z += 1
                if z >= limit:
                    continue

I actually fixed it by removing the list append before returning the var. (the loop was irrelevant at the time, now it is :))