0

I want to know how to clean ALL temporary files by using python code/direction. I cannot look at the names of FILES. Just want to clean my Temp Files Cache

Thanks in advance!

Khalid
  • 621
  • 1
  • 7
  • 14
  • Possible duplicate of [Delete a file or folder](https://stackoverflow.com/questions/6996603/delete-a-file-or-folder) – MaJoR Nov 24 '18 at 11:45
  • @MaJoR Hi. It is not duplicate question and I modify question. – Khalid Nov 24 '18 at 11:48
  • If it's not a duplicate, then tell us how and where are you creating the temporary files? You **MUST** have a path. If you have the path, then you just have to follow the above discussion on how to delete the files/folder. If it's different than this, then please edit the question, and frame it properly. – MaJoR Nov 24 '18 at 11:57
  • The problem is I am working remotely. I just join to another PC and work on it. I am not able to learn the path from this PC. – Khalid Nov 24 '18 at 11:58
  • Without a path, nothing can be done. You can guess where the cache is stored , by accessing the generic folder of the OS you are working on. For example, on Linux, cache is generally stored in `~/.cache/` – MaJoR Nov 24 '18 at 12:01

1 Answers1

0
from __future__ import print_function
from contextlib import suppress 
import os
import shutil

top_level_tempFolder = os.environ["TEMP"]
lof = os.listdir(top_level_tempFolder)
for f in lof:
    with suppress(PermissionError, FileNotFoundError):
        if os.path.isdir(os.path.join(top_level_tempFolder, f)):
            shutil.rmtree(os.path.join(top_level_tempFolder, f))
        else:
            os.remove(os.path.join(top_level_tempFolder, f))

print("done")
# os.startfile(top_level_tempFolder)

Using this will clean any files and folders that Windows is able to remove from the temp folder; and will not halt for any errors associated with permissions or missing files.

structure
  • 35
  • 7