James Stone James Stone - 14 days ago 5
Python Question

How do I search the 'Startup' folder in Windows and delete files I don't want?

Basically, what I'm trying to do is make a program that once run, saves a copy of itself to the Windows 'Startup' folder.

My friend however has made some sort of 'antivirus' (it's not that great) that checks 'Startup' - when Windows starts up - for any files with the extension

.py
or
.pyw
.

I'm trying to make a section of my program that does the same sort of thing, only it's supposed to delete his file before it has time to delete mine.

The program I'm editing is not originally mine, therefore I don't really understand a lot of it, but I have an alright knowledge about Python. I've tried this in the place I think it's supposed to go, but I get an error:

for file in os.listdir("C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/"):
if file.endswith(".pyw") or file.endswith(".py"):
os.remove(file)


This is the error I get when I have a file (
Test.py
) in the 'Startup' folder:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Test.py'


Any suggestions would be appreciated. Thanks.

Answer

Change os.remove(file) to os.remove("C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/" + file

Comments