Is anyone know how to delete all files in a Folder kodi python?
#1
Hello I'm making a Addon... I wanna delete all files in kodi database folder.
(special://database) or sdcard/Android/data/org.xbmc.kodi/files/.kodi/userdata/Database/

How to do this? Which python codes? Can anyone help?
Reply
#2
Try my add-on. This may be what you're looking for: http://forum.kodi.tv/showthread.php?tid=228075
Reply
#3
I usually use shutil.rmtree to delete folders (https://docs.python.org/2/library/shutil.html)
Reply
#4
The basic process works like

Code:
if os.path.exists( PATH ):
        log("shutil.rmtree Removing path")
        shutil.rmtree( PATH , ignore_errors=True)

where PATH is what you want to kill. That works for me for folders in addon_data anyway.

I struggle to think why you'd legitimately want to nuke the entire database folder though? I suppose some sort of cleanup script?
Addons I wrote &/or maintain:
OzWeather (Australian BOM weather) | Check Previous Episode | Playback Resumer | Unpause Jumpback | XSqueezeDisplay | (Legacy - XSqueeze & XZen)
Sorry, no help w/out a *full debug log*.
Reply
#5
(2015-08-11, 02:30)bossanova808 Wrote: I struggle to think why you'd legitimately want to nuke the entire database folder though?

+1
Reply
#6
personally I get alarmed at the idea that addons out there want to delete DB files...
Reply
#7
Does anyone know how I would delete the temp folder on initial run?

I am using the autoexec.py in userdata with the following

Code:
import os
import shutil
if os.path.exists(special://temp):
        log("shutil.rmtree Removing path")
        shutil.rmtree(special://temp, ignore_errors=true)

However nothing happens, the folder does not get deleted. Any ideas?[/code]
Reply
#8
(2016-02-04, 16:47)tdbegley Wrote: Does anyone know how I would delete the temp folder on initial run?

I am using the autoexec.py in userdata with the following

Code:
import os
import shutil
if os.path.exists(special://temp):
        log("shutil.rmtree Removing path")
        shutil.rmtree(special://temp, ignore_errors=true)

However nothing happens, the folder does not get deleted. Any ideas?[/code]

deletefolder = xbmc.translatePath(
'special://home/temp'
)

folder = deletefolder
if os.path.exists(deletefolder):
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
donevalue = '1'
except Exception, e:
print e

Try this
Reply
#9
Code:
15:48:06 T:2472   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.SyntaxError'>
                                            Error Contents: ('invalid syntax', ('C:\\Users\\Thomas\\AppData\\Roaming\\Kodi\\userdata\\autoexec.py', 11, 8, '\t\t\t\telif os.path.isdir(file_path): shutil.rmtree(file_path)\n'))
                                            SyntaxError: ('invalid syntax', ('C:\\Users\\Thomas\\AppData\\Roaming\\Kodi\\userdata\\autoexec.py', 11, 8, '\t\t\t\telif os.path.isdir(file_path): shutil.rmtree(file_path)\n'))
                                            -->End of Python script error report<--

Thanks for you help
Reply
#10
(2016-02-04, 17:49)tdbegley Wrote:
Code:
15:48:06 T:2472   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.SyntaxError'>
                                            Error Contents: ('invalid syntax', ('C:\\Users\\Thomas\\AppData\\Roaming\\Kodi\\userdata\\autoexec.py', 11, 8, '\t\t\t\telif os.path.isdir(file_path): shutil.rmtree(file_path)\n'))
                                            SyntaxError: ('invalid syntax', ('C:\\Users\\Thomas\\AppData\\Roaming\\Kodi\\userdata\\autoexec.py', 11, 8, '\t\t\t\telif os.path.isdir(file_path): shutil.rmtree(file_path)\n'))
                                            -->End of Python script error report<--

Thanks for you help

send me your py file
Reply
#11
(2016-02-04, 17:49)tdbegley Wrote:
Code:
15:48:06 T:2472   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.SyntaxError'>
                                            Error Contents: ('invalid syntax', ('C:\\Users\\Thomas\\AppData\\Roaming\\Kodi\\userdata\\autoexec.py', 11, 8, '\t\t\t\telif os.path.isdir(file_path): shutil.rmtree(file_path)\n'))
                                            SyntaxError: ('invalid syntax', ('C:\\Users\\Thomas\\AppData\\Roaming\\Kodi\\userdata\\autoexec.py', 11, 8, '\t\t\t\telif os.path.isdir(file_path): shutil.rmtree(file_path)\n'))
                                            -->End of Python script error report<--

Thanks for you help

Code:
TARGETFOLDER = xbmc.translatePath(
    'special://home/temp'
    )

path = TARGETFOLDER

folder = TARGETFOLDER
if os.path.exists(TARGETFOLDER):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path): shutil.rmtree(file_path)
            donevalue = '1'
        except Exception, e:
            print e

Send me your py file or now try this one...
Reply
#12
Thank you very much, it works flawlessly.

If i wanted to apply the same principle to the addons/packages folder would it be as simple as just copying the same cope again below but changing it to this?

Code:
TARGETFOLDER = xbmc.translatePath(
    'special://home/temp'
    )

path = TARGETFOLDER

folder = TARGETFOLDER
if os.path.exists(TARGETFOLDER):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path): shutil.rmtree(file_path)
            donevalue = '1'
        except Exception, e:
            print e

TARGETFOLDER = xbmc.translatePath(
    'special://addons/packages'
    )

path = TARGETFOLDER

folder = TARGETFOLDER
if os.path.exists(TARGETFOLDER):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path): shutil.rmtree(file_path)
            donevalue = '1'
        except Exception, e:
            print e
Reply
#13
(2016-02-04, 18:12)tdbegley Wrote: Thank you very much, it works flawlessly.

If i wanted to apply the same principle to the addons/packages folder would it be as simple as just copying the same cope again below but changing it to this?

Code:
TARGETFOLDER = xbmc.translatePath(
    'special://home/temp'
    )

path = TARGETFOLDER

folder = TARGETFOLDER
if os.path.exists(TARGETFOLDER):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path): shutil.rmtree(file_path)
            donevalue = '1'
        except Exception, e:
            print e

TARGETFOLDER = xbmc.translatePath(
    'special://addons/packages'
    )

path = TARGETFOLDER

folder = TARGETFOLDER
if os.path.exists(TARGETFOLDER):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path): shutil.rmtree(file_path)
            donevalue = '1'
        except Exception, e:
            print e

Yup... Smile
Reply
#14
Just a quick one guys.

The above script is fantastic but it disables me being able to use a log.

So how easy would it be to use the same principle above but instead of deleting the entire temp folder just deleting the .fi files within the folder?

Sort of a delete *.fi script instead of the entire folder.

Thanks in advance guys
Reply
#15
Nevermind, managed to do it.

For anyone thats interested the code is as follows

Code:
scandirs = xbmc.translatePath(
    'special://home/temp'
    )

path = scandirs
exts = ('.fi')
if os.path.exists(scandirs):
    for root, dirs, files in os.walk(path):
        for currentFile in files:
            if any(currentFile.lower().endswith(ext) for ext in exts):
                os.remove(os.path.join(root, currentFile))
Reply

Logout Mark Read Team Forum Stats Members Help
Is anyone know how to delete all files in a Folder kodi python?0