'how to remove peripheral device from devices and printers in python
I want to remove device specific device attached to my computer when i run a script in python but am not able to do this. Have searched on the web with no solution. link 1 and link 2
I learnt i can achieve that with power shell but am not able to achieve that.
This code is able to open the cd rom successfully
import os
os.system('powershell $driveEject = New-Object -comObject Shell.Application; $driveEject.Namespace(17).ParseName("""F:""").InvokeVerb("""Eject""")')
I came across this question how to remove printer from devices and printer which there is no answer for it.
I kindly need assistance achieve such result.
Solution 1:[1]
First I prepared a powershell script (s.ps1):
$driveEject = New-Object -comObject Shell.Application
$driveEject.Namespace(17).ParseName("E:").InvokeVerb("Eject")
Then I created a Python script
import os
os.system('powershell.exe -Command ./s.ps1 >log.txt')
After that I got an error in the log file
./s.ps1 : s.ps1 cannot be loaded because
running scripts is disabled on this system. For
more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ ./s.ps1
+ ~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
After reading the article I executed the commands in cmd running under the administrator
> powershell
Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.
PS > Set-ExecutionPolicy -ExecutionPolicy Unrestricted
After that, I was finally able to run the script. And I saw a pop-up in the tray with a message that I can eject the disk.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


