windows python脚本遍历目录以删除文件夹,重新启动PC并继续脚本的下一行?
我想删除错误安装的程序并重新安装。我可以使用 subprocess.Popen 调用 msiexe 来删除该程序,并以相同的方式安装新程序,但只能使用两个独立的脚本。但我还需要删除 C:\Programs 文件以及 C:\Doc& 中的一些文件夹。设置。我如何遍历目录结构并删除文件夹?另外,如何在从下一行重新启动电脑后继续执行脚本以安装新程序。
I want to remove a incorrectly installed program and reinstall it. I can remove the program with subprocess.Popen calling the msiexe on it and install new program the same way BUT ONLY with two independent scripts. But i also need to remove some folders in C:\Programs files and also in C:\Doc& Settings. How can i traverse through the directory structure and remove the folders?Also how can i continue the script after restart the PC from the next line to install the new program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,这就是您需要做的事情。
您可以使用
os< 中的
remove()
和rmdir()
或removdirs()
方法删除文件和文件夹/code> 模块(假设您的用户/程序具有管理权限)。要重新启动脚本,您首先需要向其添加一些命令行参数处理,以允许它被告知是从头开始还是从另一点继续。
要使脚本在重新启动后运行,您需要在 Windows 注册表中设置一个值。我相信它们存储在
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
和HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
键下。您可以在其中添加一个字符串值(类型REG_SZ
),其中包含一个调用脚本的命令行,并向其传递适当的命令行参数,该参数将告诉它继续并重新安装程序。In a nutshell, here's what you'll need to do.
You can delete the files and folders by using the
remove()
andrmdir()
orremovedirs()
methods in theos
module (assuming your user/program has administrative rights).To restart your script you will first need to add some command line argument handling to it that allows it to be told whether to start from the beginning or continue from the other point.
To get the script to run after restart, you'll need to set a value in the Windows registry. I believe they're stored under the
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
andHKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
keys. There you can add a string value (typeREG_SZ
) which contains a command line to invoke your script and pass it the appropriate command line argument(s) which will tell it to continue and re-install the program.