Cleaning the Prefetch folder in Windows OS helps increasing the performance of the system. You can clean your Prefetch files manually by going to the X:\Windows\Prefetch\ folder and deleting all the files in that directory. This can be time consuming and today I will be introducing the “os” module in python and show you how to create your own prefetch cleaner program so that you clean the prefetch folder for you in a single click.
So, basically the prefetch cleaner simply changes the current directory to your Prefetch folder(X:\Windows\Prefetch\ , where X is your drive where your Windows is installed) and gets the list of files in that folder and deletes each one of them.
Assuming you have python installed in your computer(else read my other blog post on how to install python here). Now, import the “os” module using the following code:
import os
The next thing you need to do is to change your current working directory to the prefetch directory. This can be done using the function called “chdir(path)” in the os module. The parameter path is going to be the prefetch directory which in my case is “C:\WINDOWS\Prefetch“. So, we we use the following line of code:
os.chdir("C:\WINDOWS\Prefetch")
Now, you need to get the list of all the files in that directory .This can be done using the function listdir(path) which returns a list containing the list of files in that directory and storing the result to a variable( For example: prefetch):
prefetch=os.listdir("C:\WINDOWS\Prefetch")
Now, since we have the list of files, to delete each file in the list you can simply use a for loop to traverse through each element in the list and call a function that deletes the file. Now, we create a function called del_file(name) which takes takes a “filename” as parameter and deletes the file. We can delete a file using the function os.remove(filename) . The function can be implemented as:
def del_file(name):
os.remove(i)
print i, " Deleted"
Now, finally you need to just create a for loop to traverse through all the elements(which are filenames of each file in the directory) in the variable prefetch and call the function del_file(i).
for i in prefetch:
del_file(i)
Complete source code of the Prefetch Cleaner:
# Prefetch Cleaner
# https://extr3metech.wordpress.com
import os
def del_file(name):
os.remove(i)
print i, " Deleted"
os.chdir("C:\WINDOWS\Prefetch")
prefetch=os.listdir("C:\WINDOWS\Prefetch") # Change letter "C" to your OS drive letter
for i in prefetch:
del_file(i)
print "Prefetch Cleaning Complete "
raw_input("Press Enter to exit..")
It is recommended that you close all media players such as VLC or Windows Media Player etc before you run your prefetech cleaner program. Happy Coding!
ΞXΤЯ3МΞ