How to delete everything inside a certain folder

I’ve tried many codes none of them work and I get the error I want to delete what’s inside the folder for example %temp%

Check out shutil.rmtree.

And in Python environment variables come from os.environ, not from %whatever%.

I know that in python you have to use entire directories But do you know any ready code?

Bearing in mind that that will delete the folder itself, not just the
internal contents.

I know that in python you have to use entire directories

I don’t know what you mean here. The shutil.rmtree function will
delete an entire directory, but there are other approaches.

But do you know any ready code?

That depends on exactly what you want to do. If you need to clean
%temp% without remove the top directory you will need to enureate the
things inside it (eg using listdir or fnglob) and remove those
things individually.

the folder also goes from F? Is there any command that just deletes what’s inside? more normally windows restores the %temp% and perhaps it would not be possible to delete it entirely because of the files in use the command should not delete the entire folder, right?

the folder also goes from F?

What’s F?

Is there any command that just deletes what’s inside? more normally
windows restores the %temp% and perhaps it would not be possible to
delete it entirely because of the files in use the command should not
delete the entire folder, right?

Off the shelf? No, or not that I know of.

Usually people want to delete only things they’ve made themselves,
inside a programme. For example, see the tempfile.TemporaryDirectory
context manager, which makes a scratch directory and cleans it up after
use.

OTOH, if you’re wanting to make a utility for emptying a folder, you’ll
have to enumerate what to remove because there’s isn’t (in the stdlib) a
remove-just-the-contents-of-this-folder. So you’d need to make one. It
would not be very difficult.

But first think: what’s your motivating use case? Is this really a good
idea? Enough to need to write a Python function for it rather than just
doing whatever is the Windows equivalent of the UNIX command:

 rm -r "$TMPDIR"/*

occasionally. (I’m taking the UNIX $TMPDIR environment variable to be
the rough equivalent of your %temp%; adjust for correctness.)

Cheers,
Cameron Simpson cs@cskk.id.au

F in chat gamer community started using the term to pay condolences. Whether it’s when someone known to the public dies, or there’s a story about a player passing away, it’s common to see people.

Is it difficult to do this? Create this code?
Goal: create a PC cleaner with an interface that deletes unnecessary files via python I don’t care about the difficulty

If you learn by doing…

F in chat gamer community started using the term to pay condolences.
Whether it’s when someone known to the public dies, or there’s a story
about a player passing away, it’s common to see people.

Ah. A new term for me. Do you know its derivation?

Is it difficult to do this? Create this code?

No.

Goal: create a PC cleaner with an interface that deletes unnecessary files via python I don’t care about the difficulty

The hard bit is enumerating what should be removed, in particular
avoiding things which shouldn’t be removed. What if something’s using
the stuff in %temp%? And so forth.

If you learn by doing…

You definitely learn by doing. Do a bit of reading, then implement
something small (make sure it has a “do nothing” mode!) and see how
good/bad it feels. Since you’re removing fines, make sure you’ve got a
recent backup!

After you’ve done a basic implementation, then you have a fuller picture
of the task and more reading or experimentation is then profitable.
Later, rinse, repeat.

Cheers,
Cameron Simpson cs@cskk.id.au

How to enumerate the amount of files that will be deleted? I don’t understand do you have an example?

I don’t know about the derivation of the F term in the chat

See the os.walk function:

That would let you walk the entire directory tree under a folder.

Have you tried to write any Python code for this at all yet?

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

yes I’ve tried many just 2 codes worked but not and what I’m looking for it just deleted a file.Then it gave an error but I was using the
shutil.rmtree()
Code:

import os
import glob

files = glob.glob(os.path.join(‘C:\Users\Windows\AppData\Local\Temp’))
files = glob.glob(os.path.join(‘C:\Users\Windows\AppData\Local\Temp’))
for file in files:
os.remove(file)

Error:1 file was tried to delete but it was being used by the system and then comes access denied

The paths shown in that code are not valid, due to string escaping. Please see:

I have another code
the code seems to work but it shouldn’t delete just one file it would be all files in the folder
erro:
PS C:\Users\Windows\Desktop> & C:/Users/Windows/AppData/Local/Programs/Python/Python311/python.exe c:/Users/Windows/Desktop/py/sad.py
Traceback (most recent call last):
File “c:\Users\Windows\Desktop\py\sad.py”, line 2, in
shutil.rmtree(‘C:\Users\Windows\AppData\Local\Temp’)
File “C:\Users\Windows\AppData\Local\Programs\Python\Python311\Lib\shutil.py”, line 759, in rmtree
return _rmtree_unsafe(path, onerror)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Windows\AppData\Local\Programs\Python\Python311\Lib\shutil.py”, line 622, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File “C:\Users\Windows\AppData\Local\Programs\Python\Python311\Lib\shutil.py”, line 620, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 32] O arquivo já está sendo usado por outro processo: ‘C:\Users\Windows\AppData\Local\Temp\0919b499-1dcf-4b98-a1d1-88eef7d1f24e.tmp’

CODE:

import shutil
shutil.rmtree('C:\\Users\\Windows\\AppData\\Local\\Temp')
import os

folder = 'C:\\Users\\Windows\\AppData\\Local\\Temp'
fileList = os.listdir(folder)

for f in fileList:
    filePath = folder + '/'+f

    if os.path.isfile(filePath):
        os.remove(filePath)

    elif os.path.isdir(filePath):
        newFileList = os.listdir(filePath)
        for f1 in newFileList:
            insideFilePath = filePath + '/' + f1

            if os.path.isfile(insideFilePath):
                os.remove(insideFilePath) 

As it tells you: Windows will not allow you to delete everything in the folder, because other programs are trying to use some files that are in the folder. It will be the same problem no matter what code you write for that deletion.

I know that Windows will not allow me to delete the entire folder, but I put some .exe in there to be deleted and none were in use and there could also be a problem in the code.Why is he only quoting 1 file in the terminal

Because it will try to delete the files one at a time, and that is the first file that caused a problem when trying to delete it.

but why then didn’t he go to the next file?

Because this raises an exception, and the program did not catch the exception.