I have a .py file. I am creating a .bat file which when clicked runs the python script & saves the output as a text file in the same directory.

I gave this inside the batch file

%CD%\App1.py > %CD%\op.txt

but I am not able to see the output nor the saved text file.

So how do I go about doing this?


You could add these two lines before invoking python

echo %CD%

Adding a pause at the end as well would be good, in case it is reporting errors.

Also, calling it a .cmd file would be preferable to a .bat file.
IIRC, there's some historic "backward compatibility" associated with .bat files, one of which may be that %CD% is undeclared.

$ set /?
If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up in
the list of variables displayed by SET.  These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:

%CD% - expands to the current directory string.

Also do cmd /? to find out how to enable "Command Extensions"

Thank you for ur reply. :)

I turned on the Command Extensions.

But I figured out that I need not use the %CD% at all as long as the batch file & the .py file are going to remain in the same directory.

So I used the following code to get the output as well as get it saved to a text file.

echo off
App1.py > App1_OUTPUT.txt
This question has already been answered. Start a new discussion instead.