0

When I write some file with .py extension and containing the following code:

import subprocess, platform

cmd = "echo Hi"

subprocess.call(cmd, shell = True)

it will return "Hi" when I double-click the file in Windows or when I write a .bat file which makes Python execute the script.

However, what I actually want to do is to shut down the computer (by executing the Python script from a batch file).

To this end, I wrote the following code:

import subprocess, platform

cmd = "shutdown /s /t 1" if platform.system() == "Windows" \
    else "systemctl poweroff"

subprocess.call(cmd, shell = True)

However, this will start to endlessly run the script without shutting down the computer. I.e., double-clicking the .py file opens a prompt which is continuously populated with the line C:\Users\... py myscript.py.

If I open a command prompt and enter

> py
> import subprocess
> subprocess.call("shutdown /s /t 1")

the computer is shut down as expected.

So, how is this different from executing the script? Is this some security issue? And how can I get this to work?

I also tried os.system(), as well as entering the subprocess call as a list (setting cmd = ["shutdown", "-s", "-t", "1"]). All behaved similarly: The code works when executed from a Python prompt but it does not when I run Python from a batch file or by double-click. In those instances, the call to echo, however, does work.

Manuel Popp
  • 1,003
  • 1
  • 10
  • 33
  • If `py` is a batch file itself, you may want to write out `python.exe` for a test. With full path if necessary. – tevemadar Jun 05 '23 at 08:10
  • 1
    A simpler problem would be if your batch file is called `shutdown.bat`, in that case write out `shutdown.exe` in the Python code. – tevemadar Jun 05 '23 at 08:13
  • @tevemadar `py.exe` is part of the Windows installation of Python. It runs one of multiple installed Python versions based on command line argument or the shebang line in the Python script. – Michael Butscher Jun 05 '23 at 08:17
  • I don't think the problem is related to the Python executable called, since it works just fine when I make Python execute the system command ```echo```. This is what confuses me. – Manuel Popp Jun 05 '23 at 08:29
  • People do what you want with identical code to yours - you can find some alternative magics if you want, but calling `shutdown` seems to work too: https://stackoverflow.com/questions/14764126/how-to-make-a-python-script-which-can-logoff-shutdown-and-restart-a-computer https://stackoverflow.com/questions/67342897/is-there-a-command-in-python-for-instant-shutdown-on-windows-10 https://stackoverflow.com/questions/34039845/how-to-shutdown-a-computer-using-python – tevemadar Jun 05 '23 at 08:57
  • @tevemadar The issue is related to how I execute the code, not to the code itself: All those examples work quite well, except if I do not run the code in a Python prompt. And what confuses me even more is, that the methods by which I execute the code (writing the Python code to a file and calling ```py somefile.py``` from a batch script) apparently also work in general, but not in this particular case... – Manuel Popp Jun 05 '23 at 09:18

0 Answers0