How to determine if Python script was run via command line?

前端 未结 11 1699
心在旅途
心在旅途 2020-12-15 03:58

Background

I would like my Python script to pause before exiting using something similar to:

raw_input(\"Press enter to close.\")

but

11条回答
  •  不思量自难忘°
    2020-12-15 04:37

    Although this isn't a very good solution, it does work (in windows at least).

    You could create a batch file with the following contents:

    @echo off
    for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set DOUBLECLICKED=1
    start 
    if defined DOUBLECLICKED pause
    

    If you want to be able to do this with a single file, you could try the following:

    @echo off
    setlocal EnableDelayedExpansion
    set LF=^
    
    
    ::  The 2 empty lines are necessary
    for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set DOUBLECLICKED=1
    echo print("first line of python script") %LF% print("second and so on") > %temp%/pyscript.py
    start /wait console_title pyscript.py
    del %temp%/pyscript.py
    if defined DOUBLECLICKED pause
    

    Batch code from: Pausing a batch file when double-clicked but not when run from a console window? Multi-line in batch from: DOS: Working with multi-line strings

提交回复
热议问题