0

I am trying to make a program in Python that will delete all files in the %temp% path, also known as C:\Users\User\AppData\Local\Temp.

How can I do this? I am using Python 3.4.

3

1 Answer 1

2

In general, you could use shutil.rmtree() to delete all files/directories in a folder:

#!/usr/bin/env python
import shutil
import tempfile

dirpath = tempfile.mkdtemp()
try:
    # use the temporary directory here
    ...
finally:
    shutil.rmtree(dirpath) # clean up

The above can be written simpler if it is all you need (create a temporary directory from scratch):

#!/usr/bin/env python3
import tempfile

with tempfile.TemporaryDirectory() as dir:
    print(dir.name) # use the temporary directory here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.