0

I want to run a batch file that uses a python script. The python script needs a date as key input.

I tried the following based on what I saw here (Sending arguments from Batch file to Python script)

@echo off
set /p data_valori=Insert date (format yyyy/mm/dd) :
"C:\Program Files\Anaconda3\python.exe" "S:\pricing\Python\new scarico Market Data ASW_PER_DATA_CUSTOM.py" %data_valori%

My python script then does the following:

import sys
data_valori = str(sys.argv[1])

I manage to insert the input in the cmd window but then it never calls python (from what I see) and it just closes the cmd window.

I tried different options for calling python, such as

python new scarico Market Data ASW_PER_DATA_CUSTOM.py %data_valori%

It still closes the cmd after inputting.

  • `set /p data_valori=Insert date (format yyyy/mm/dd) :` should really be `set /p "data_valori=Insert date (format yyyy/mm/dd) :"`, its content should be evaluated and validated after the input has been provided, because your end user cannot be expected to only ever follow the prompted format, _(deliberately or accidentally)_. `%data_valori%` may be better passed as `"%data_valori%"`, and I would have expected that your input would be agument index `0`, not argument index `1`. – Compo Apr 27 '21 at 15:39
  • I cannot see anything in your batch file code which prevents it, from closing after having run your command, so if you're running the batch script from the GUI, then of course the `cmd.exe` window will close. – Compo Apr 27 '21 at 15:47
  • 2
    @Compo sys.argv[0] is always the script name by python docs. – Emanuele G Apr 27 '21 at 15:49
  • **1.** And just to provide you with another option - unless your bat file has something else important being done in it, you could also just have the Python script ask for the date: `data_valori = input("Insert date (format yyyy/mm/dd)")`. **2.** Btw, to prevent the batch file window from auto-closing after it has finished running, add this at the end: `pause` ([see this answer](https://stackoverflow.com/a/988423/1431750)). **3.** `sys.argv` values are always strings, so you don't need `str(sys.argv...)`. – aneroid Apr 27 '21 at 15:51
  • @EmanueleG, instead of just commenting on one single part of my comment, why not try changing the other lines, I mentioned, and either add a last line `pause` command or run your script from the CLI, preferably without `@echo off`, and read the console output. – Compo Apr 27 '21 at 15:53
  • @aneroid how would you do 1? I tried that with input() but I am not sure how to reflect that in the cmd window of the bat. – Emanuele G Apr 27 '21 at 15:54
  • After `data_valori = input("Insert date (format yyyy/mm/dd)")` if you did a `print(data_valori)` then you'd see it in your command window. **Add a `pause`** in the bat file to stop the window from closing once done, so you can see the output before it closes. Also, run your bat file from the **command prompt _first_** so you can at least confirm that your scripts are running correctly. You can use the "double click the batch file" method _after_ you know that it's working. – aneroid Apr 27 '21 at 15:57
  • 1
    aneroid Yes that worked, I don't know why it was not showing before in my cmd. @Compo yes, adding pause helped catching the error. By the way, the issue was not with the input itself, but how that input was then called later on from the python script into Excel. I tested both way of calling the input (set from .bat or input() from python, and they both worked. Thank you everyone. – Emanuele G Apr 27 '21 at 16:18
  • @EmanueleG Glad it helped. In that case, this question can be closed. – aneroid Apr 27 '21 at 16:19

0 Answers0