简体   繁体   English

使用Python在Windows中删除快捷方式

[英]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 ,用于运行Python脚本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应该在启动时运行,因此在C:\\ Users \\ abhishek.roy \\ AppData \\ Roaming \\ Microsoft \\ Windows \\ Start Menu \\ Programs \\ Startup中具有快捷方式

abc.bat : abc.bat

@echo off
python xyz.py

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. Python程序应运行15天,并应在15天后从Startup文件夹中删除该快捷方式,以便abc.bat在启动时停止运行。 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 . 我一直在尝试os.remove("C:\\Users\\abhishek.roy\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\abc.bat")但它给出了FileNotFoundError

Please note that I do not want to delete the original abc.bat . 请注意,我不想删除原始的abc.bat I only want to delete the shortcut from the Startup folder.I would like to request any possible solutions. 我只想从Startup文件夹中删除快捷方式。我想请求所有可能的解决方案。

Thanks 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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