That is, I need to write code that removes a specific program, preferably without user intervention.
The program to be removed is not wmic (i.e. not installed by the Windows installer), so, for example, this code will not work:
import os
path = 'file.bat'
with open (path, 'w') as f:
s = 'wmic product where name = "{}" call uninstall /nointeractive'.format('Program name')
f.write (s)
os.startfile (path)
As I understand it, you also need to use a bat-file, but you need a different command. Probably.
I would be very grateful for your help !!
Answer 1, authority 100%
- Use os.remove (“path to file”) – delete file
- os.rmdir (“path to file”) – delete empty folder
- shutil.rmtree (“path to file”) – deleting a folder with files
Note: python only accepts paths that do not have a single \ – (backslash). For example, it will accept the path D: / folder / file or C: \\ folder \\ file. But not will accept D: \ folder \ file.
And so, an example of using shutil.rmtree ():
import shutil
shutil.rmtree ("D: / 111 / new")
If you want to delete something via a .bat file, you need to create a .bat file and write the command to it:
(choose the one that suits you best)
- del “file address” (deletes a single file or all files from a folder, keeping the folder)
- rd “file url” (deletes folder)
- rmdir does the same as rd
You can add at the end of the line or after the command itself:
- / s – all attachments will be removed (otherwise you will get
notification the folder is not empty and it will not be deleted) - / Q deletes files without confirmation. Otherwise, a window will appear:
D: \ 111 \ New folder, are you sure [Y (yes) / N (no)]?
Command example:
rd "D: \ 111 \ New folder" / s / Q
The file is ready, now you need to run it through python. To do this, use subprocess
import subprocess
subprocess.Popen ('file address')
Popen is not a typo, the command comes from the words “process open”
You will need to write down the paths manually or enter the correct paths that python can understand, or add the r prefix to the beginning of the line. Example: shutil.rmtree (r "C: \ Users \ User \ Desktop \ New Folder")