Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with existing files
#1
Question 
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!
Reply
#2
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')
Reply
#3
What is the dozip() call on line 30 for? Could it happen continually?
Reply
#4
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
Reply
#5
(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
Reply
#6
(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.
Reply
#7
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
Reply
#8
(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 :))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open files in an existing window instead of new Kostov 2 332 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Add\insert header row existing csv files mg24 0 727 Oct-05-2022, 06:11 AM
Last Post: mg24
  Working with excel files arsouzaesilva 6 3,196 Sep-17-2021, 06:52 PM
Last Post: arsouzaesilva
  filecmp is not working for UTF-8 BOM encoded files sureshnagarajan 3 2,620 Feb-10-2021, 11:17 AM
Last Post: sureshnagarajan
  Unrelated new function causing existing working function to fail Nick_G 2 2,277 Jan-27-2020, 07:21 PM
Last Post: Nick_G
  working with more excel files Krszt 1 2,469 Mar-13-2019, 11:41 AM
Last Post: perfringo
  Working with files and dictionaries OmarSinno 1 2,613 Oct-30-2017, 11:02 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020