Delete temp files and empty recycle bin I was wondering if someone could help critique this code I put together. It is supposed to delete all temp files and folders and then empty the recycle bin. I had to make some adjustments because using Pyinstaller, I wanted the console to stay open and have the user proceed after reading.

import os
file_size = os.path.getsize('C:\Windows\Temp')
print(str(file_size) + "kb of data will be removed")
import os, subprocess
del_dir = r'c:\windows\temp'
pObj = subprocess.Popen('rmdir /S /Q %s' % del_dir, shell=True, stdout = 
subprocess.PIPE, stderr= subprocess.PIPE)
rTup = pObj.communicate()
rCod = pObj.returncode
if rCod == 0:
    print('Success: Cleaned Windows Temp Folder')
else:
    print('Fail: Unable to Clean Windows Temp Folder')
import winshell
winshell.recycle_bin().empty(confirm=False, show_progress=False, 
sound=False)
from random import randint
from time import sleep
sleep(randint(4,6))
input("Press any key to continue")
I'm not sure this answer will be sufficient, but some quick things that jump out:


Keep your code organized:


Put imports at the top
Guard any serious code execution in a __name__ == '__main__' block (and usually use a main function as well)
Have blank space every few lines to visually separate logical groups of code
If the code doesn't document itself (and ideally, it should), add comments where necessary

Use consistent, conventionally Pythonic naming, such as using lower-case snake-case for variable names (e.g., return_code instead of rCod)
As a super minor detail, there are better ways to construct strings than adding them. A fairly modern, flexible way to do so is to use .format, or if you can be sure you're using Python 3.6+ then "f-strings" are wonderful)
It's really not clear why you sleep for a random amount of time at the end... If that's important, comment why so, otherwise probably don't do it. If there's something specific you're waiting on, figure out how to wait for it properly so you don't have a possible race condition.
I'm not super familiar with Windows commands and what /S /Q do to the rmdir command, but could you not just use os.rmdir(del_dir)?


Here's what your code might look like using the above suggestions:

import os
import subprocess
import winshell
from random import randint
from time import sleep

def main():
    file_size = os.path.getsize('C:\Windows\Temp')
    print("{} kb of data will be removed".format(file_size))
    del_dir = r'c:\windows\temp'

    # Could this just be os.rmdir(del_dir)???
    process = subprocess.Popen('rmdir /S /Q {}'.format(del_dir), shell=True,
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    _ = process.communicate()
    return_code = process.returncode
    if return_code == 0:
        print('Success: Cleaned Windows Temp Folder')
    else:
        print('Fail: Unable to Clean Windows Temp Folder')

    winshell.recycle_bin().empty(confirm=False, show_progress=False, sound=False)

    # Is this important?
    # sleep(randint(4, 6))
    input("Press any key to continue")

if __name__ == '__main__':
    main()