0

Hello I Wish to run a python script, (I did not write), from a batch file I wrote.

I have 2 questions:

  1. Running the command in the Command prompt works fine but from the batch file it fails with no error message; the python script creates an output file but it is not created when running through the batch file.

    @echo off
    SET /P PrePRD=Enter the Pre Production file name: 
    SET /P PostPRD=Enter the Post Production file name: 
    Set /p IdColumn= Enter the Name of the ID column (ID - Original Input):
    
    cmd /k "cd /d C:\CSVComparison"
    C:\CSVComparison\csv_diff.py -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"
    
  2. I wish to open the output file which python created using Notepad++, but I'm not sure it will work this way:

    start C:\Program Files (x86)\Notepad++\notepad++.exe C:\CSVComparison\results_details.json
    notepad++.exe C:\CSVComparison\results_details.json
    

Thank you,

2 Answers 2

1

1) you start another instance with cmd /k, but the following command gets executed in the original instance (I guess, you want it to be executed in the second instance). Either execute it in the original instance (no cmd /... at all; you might need to call it)

pushd "C:\CSVComparison"
call "C:\CSVComparison\csv_diff.py" -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"
popd

or start it in another window:

start /d "C:\CSVComparison" "csv_diff" "C:\CSVComparison\csv_diff.py" -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"

2) if a folder or file contains space(s) , you need to enclose the whole thing in quotes. Better: get used to always enclose folders/filenames in quotes:

start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\CSVComparison\results_details.json"

(start) takes the first quoted string as window title, so you need to give it a fake title ("")

1

Answering your first question - unfortunately Windows does (yet?) not know about shebangs, so you need to specify that you want to run your python script using python interpeter:

cmd /k "cd /d C:\CSVComparison"
C:\path\to\python.exe C:\CSVComparison\csv_diff.py -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"

Not the answer you're looking for? Browse other questions tagged or ask your own question.