Justaus3r
Justaus3r

Reputation: 423

How to shutdown pc in python without using system commands

As the title suggests i want to shutdown pc without using modules such as os ,popen or subprocess to invoke system commands.i have searched alot but all the answers were using os module to invoke system commands.i want a pure python way of doing this.and also OS independent.Any help would be greatly appreciated!

Upvotes: 0

Views: 127

Answers (2)

This operation will always include operating system calls ,cause anyways you ask for an operating system action.A pure python module that would do the thing you ask ,anyways will use the things you want to avoid.So yes there is a way to do it with 'pure python' but you need to write the code for your case as i dont think any library exists by now(due to complexity for all cases for all actions). The solution is pretty straight forward:

  • Define what os system you work with platform module(platform.system(),platform.release(),platform.version())
  • Write the os system calls for each platform.

Upvotes: 1

TomM
TomM

Reputation: 175

Much of the code that you write is actually being managed by the Operating System, and doesn't run independently from it. What you are trying to accomplish is a protected action, and needs to be invoked by the OS API.

If I understand correctly, programming languages like Python can't usually directly work with a computer's hardware, and even much more low level programming languages like C require use of the Operating System's APIs to take such action.

That's why most of the solutions you've found depend on the os package, Python doesn't have the ability to do it natively, it needs to make use of the aforementioned OS API.

This is a feature, not a bug, and helps keep programs from gaining access or visibility into other processes and protected operations.

Upvotes: 1

Related Questions