pythonpython-3.xoperating-systemsys

Problem deleting files/folders using Python


Now I'm sitting and making a Python program to clean up junk on my PC. And everything would be fine, but one problem appeared. The fact is that I cannot delete files from the “Temp” folder of my profile (the first folder from which files need to be deleted, by the way).

At first I tried to do it like this:

os.system(f'cd C:/Users/{current_user}/AppData/Local/Temp')
os.system(f'del /s /q *')

However, not only did it not work, I suddenly almost killed my laptop, because for some reason the command to delete files extended not to a specific folder, but to the ENTIRE DISK.

The second time I did this code:

os.system(f'cd C:/Users/{current_user}/AppData/Local/Temp')
os.remove(f'C:/Users/{current_user}/AppData/Local/Temp/')

But after running it in the command line (it was enabled in administrator mode at that time), the following error is displayed:

Traceback (most recent call last):
  File "C:\Users\Home\PycharmProjects\mainProject\GlowClean.py", line 46, in <module>
    easy_clean()
  File "C:\Users\Home\PycharmProjects\mainProject\GlowClean.py", line 42, in easy_clean
    os.remove(f'C:/Users/{current_user}/AppData/Local/Temp/')
PermissionError: [WinError 5] Отказано в доступе: 'C:/Users/Home/AppData/Local/Temp/'

Translation: "Отказано в доступе" translated as "Access denied"

What could be my mistake? For a more detailed answer, I’m even ready to insert the full program code into this post.

P.S. Don't judge me harshly because I don't know the obvious, I'm still new to Python.


Solution

  • What could be my mistake?

    Your first mistake was trying to use os.system() for this purpose. Python has built-in functions for manipulating files and folders, as well as some good third-party modules. Use them (more on that later).

    When you do use os.system(), you need to be aware that each call executes the specified command in its own, isolated shell context. Among the implications is that if you cd to a different working directory or modify environment variables then those changes are visible only to the command(s) you are running in that one os.system() call. Thus, your

    os.system(f'cd C:/Users/{current_user}/AppData/Local/Temp')
    os.system(f'del /s /q *')
    

    is functionally equivalent to just

    os.system(f'del /s /q *')
    

    alone. That's unlikely to be what you want. What it actually tries to delete depends on the working directory for the host python.

    As far as your second attempt goes, again, using os.system() to change the working directory is pointless, but not inherently erroneous. The issues here are that

    1. os.remove() applies to regular files only, not directories
    2. os.rmdir() is the analog for directories, but it applies only to empty directories.
    3. To remove a whole directory tree, you could use shutil.rmtree(), but
    4. You do not want to do this. C:/Users/{current_user}/AppData/Local/Temp/ contains temporary files for everything {current_user} may be running at the time. It is not safe to blindly delete its contents, and the folder itself should not be deleted in any case.