1

Considering this batch file : "c.bat"

echo "Ok"
TIMEOUT /T 5 /NOBREAK > nul

and this python script : "main.py"

import os
filepath="C:/Users/david.rouyre/Desktop/c.bat"
os.system(f"start /wait {filepath}")

All works fine when I run the script. But when I run it with VSCode, i have the following error for TIMEOUT command :

C:\Users\david.rouyre>echo "Ok"
"Ok"

C:\Users\david.rouyre>TIMEOUT /T 5 /NOBREAK  1>nul
TIMEOUT: invalid time interval ‘/T’
Try 'TIMEOUT --help' for more information.

C:\Users\david.rouyre>pause
Appuyez sur une touche pour continuer...

Except the TIMEOUT error, the two cmds windows are similar. In both cases i use python 3.9.10 (i also tested 3.7)

I tried using subprocess.Popen but it's the same result.

Do you have any idea to resolve this problem ?

David Rouyre
  • 68
  • 1
  • 6
  • 1
    How about changing ```TIMEOUT /T 5 /NOBREAK 1>nul``` to ```%SystemRoot%\System32\timeout.exe /T 5 /NoBreak 1>NUL```? – Compo Jan 16 '23 at 16:13
  • There seems to be two slightly different implementations of `TIMEOUT` in Windows. I haven't figured out the conditions in which one gets used instead of the other - but it appears that if you use `-T` instead of `/T` for the time parameter, the command will work with either version. – jasonharper Jan 16 '23 at 17:16
  • The main mistake here is the usage of Python function [os.system](https://docs.python.org/3/library/os.html#os.system) which is deprecated since many years and should not be used anymore in newly written Python scripts. `os.system()` calls the [system](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem) function which on Windows calls the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa) to run `%ComSpec% /c` with the command linen appended as arguments. – Mofi Jan 16 '23 at 17:36
  • That causes often troubles because of `%ComSpec` is an environment variable defined with `%SystemRoot%\system32\cmd.exe` resulting in running either 64-bit `C:\Windows\System32\cmd.exe` or 32-bit `C:\Windows\SysWOW64\cmd.exe` depending on Python script interpreted by 64-bit or 32-bit `python.exe` on 64-bit Windows. `cmd.exe` has a special interpretation of the command line (arguments) after option `/c` as explained by its usage help output on running `cmd /?` in a command prompt window. That requires often special syntax for the command line to fulfill the requirements of `python` and `cmd`. – Mofi Jan 16 '23 at 17:40
  • `cmd.exe` uses for each executable specified on the command line itself the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) after having found the executable at all as explained by [this answer](https://stackoverflow.com/a/41461002/3074564). The usage of `os.system()` can cause complex command line string syntax in Python script and dependencies on `cmd.exe` and the __local__ environment variables `PATH` and `PATHEXT`. – Mofi Jan 16 '23 at 17:43
  • There should be used in Python scripts the [subprocess module](https://docs.python.org/3/library/subprocess.html) which is on Windows a Python wrapper module of `CreateProcess` which is called directly by `python.exe` to run an executable without or with usage of a [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure. A Python script writer has with `os.system()` no control over how an executable is run while the script author has full control over execution of a program using `subprocess.Popen()`. – Mofi Jan 16 '23 at 17:45
  • However, the execution of the Windows command `%SystemRoot%\System32\timeout.exe` with `os.system()` or directly with `subprocess.run()`, `subprocess.call()` or `subprocess.Popen()` is never necessary at all because of native Python code can be used for a time delay. The example is therefore not really good. There is also never the need to run a batch file processed by `cmd.exe` using a Python script. Python is a much more powerful script interpreter than `cmd.exe`. Whatever command lines are in a batch file for a task, the same task can be done with pure native Python code. – Mofi Jan 16 '23 at 17:49
  • 2
    BTW: The error message posted in question is not output by `%SystemRoot%\System32\timeout.exe`. It is output by a different program/script with name `timeout` which was found first by `cmd.exe` instead of `%SystemRoot%\System32\timeout.exe`. Your __system__ environment variable `PATH` and most likely also your __user__ environment variable `PATH` are most likely already defined wrong causing the execution of wrong `timeout` program/script. You should fix that as soon as possible. – Mofi Jan 16 '23 at 17:52
  • Thanks, it's more clear. Then i used `%SystemRoot%\System32\timeout.exe` to specify exactly the "TIMEOUT" i want to use. – David Rouyre Jan 18 '23 at 09:32

0 Answers0