简体   繁体   中英

Deleting shortcuts in Windows using Python

I have a batch file abc.bat which is used to run a Python script xyz.py

abc.bat should run at startup and therefore has a shortcut in C:\\Users\\abhishek.roy\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup

abc.bat :

@echo off
python xyz.py

xyz.py :

import datetime
import os

if int(str((datetime.date.now() - datetime.date(2017,12,11))).split()[0]) <= 15
    # Do something
else:
    # Delete the shortcut from the startup folder
    # Need code here

The Python program should run for 15 days and after 15 days it should delete the shortcut from the Startup folder so that abc.bat stops running at startup. I have been trying os.remove("C:\\Users\\abhishek.roy\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\abc.bat") but it gives a FileNotFoundError .

Please note that I do not want to delete the original abc.bat . I only want to delete the shortcut from the Startup folder.I would like to request any possible solutions.

Thanks

由于它是批处理文件的快捷方式 ,而不是文件本身的快捷方式 ,因此只需将os.remove() .bat更改为.lnk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM