Skip to content Skip to sidebar Skip to footer

How Do I Delete A (g)zip File In Windows Via Python? (file Generated In Labview.)

I have a few zip files that I need to delete programmatically in Python 3. I do not need to open them first at all: I can determine if I want to delete them solely based on their f

Solution 1:

I believe the solution is in addressing the root cause of the error you get:

PermissionError: [WinError 32] The process cannot access the file because it 
is being used by another process: 'C:\\Users\\akeister\\Desktop\\Tatsuro 1.2.0.zip'

It appears to not be so much of a Python problem as much as it appears to be a Windows and/or LabVIEW problem.

It's normal behavior for a file to not be able to be deleted if another process has a lock on it. So, releasing the lock (which it seems LabVIEW is still holding) is a must.

I recommend identifying the LabVIEW PID and either stopping or restarting LABVIEW.

You might try incorporating https://null-byte.wonderhowto.com/forum/kill-processes-windows-using-python-0160688/ or https://github.com/cklutz/LockCheck (a Windows based program which identifies file locks).

If you can incorporate either into your Python program to shutdown or restart the locking process then the zip file should be removable.

Solution 2:

For completeness, I will post the code that worked (after completely closing down LabVIEW):

import shutil
import os
import tkinter as tk


""" ----------------- Get delete_zip_files variable. --------------- """


delete_zip_files = False

root= tk.Tk() # create windowdefdelete_button():
    global delete_zip_files

    delete_zip_files = True
    root.destroy()

defleave_button():
    global delete_zip_files

    delete_zip_files = False
    root.destroy()


del_button = tk.Button(root, text='Delete Zip Files',
                   command=delete_button)
del_button.pack()  

lv_button = tk.Button(root, text='Leave Zip Files',
                  command=leave_button)
lv_button.pack()                     

root.mainloop()

print(str(delete_zip_files))


""" ----------------- List files in user's desktop. ---------------- """# Desktop path is os.path.expanduser("~/Desktop")# List contents of a directory: os.listdir('directory here')
desktop_path = os.path.expanduser("~/Desktop")

for filename in os.listdir(desktop_path):
    if filename.startswith('Tatsuro') or \
            filename.startswith('TestScript'):
        # Get full path to file.
        file_path = os.path.join(desktop_path, filename)

        if filename.endswith('2015'):
            # It's a folder. Empty the folder, then delete the folder:
            shutil.rmtree(file_path)

        if filename.endswith('.zip'):
            # Get desired folder name. 
            target_folder_name = filename.split('.zip')[0]
            target_folder_path = os.path.join(desktop_path, 
                                              target_folder_name)

            # Now we process. Unzip and delete.
            shutil.unpack_archive(file_path, target_folder_path)

            if delete_zip_files:

                # Now we delete if the user chose that.
                os.remove(file_path)

Post a Comment for "How Do I Delete A (g)zip File In Windows Via Python? (file Generated In Labview.)"