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.

9
  • 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. Commented Apr 27, 2021 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. Commented Apr 27, 2021 at 15:47
  • 2
    @Compo sys.argv[0] is always the script name by python docs. Commented Apr 27, 2021 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). 3. sys.argv values are always strings, so you don't need str(sys.argv...). Commented Apr 27, 2021 at 15:51
  • 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. Commented Apr 27, 2021 at 16:18

1 Answer 1

2

Adding pause at end of batch script helped catching the error in the python script

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.