Hello

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?

Thanks

Dani AI

Generated

A reliable, maintainable approach is to make the batch file switch to its own folder, invoke the Python interpreter explicitly, and capture both standard output and standard error. That covers the common causes hinted at by (command‑extension/working‑directory issues) and the behaviour observed when running the script by name.

@echo off
pushd "%~dp0"
py -3 -u "App1.py" > "App1_OUTPUT.txt" 2>&1
popd

Notes on the lines above: pushd "%~dp0" sets the current directory to the folder containing the batch file so the output file will be created next to the script. py -3 -u uses the Windows Python launcher for the default Python 3 and the -u flag forces unbuffered output (useful if the script prints progressively). The > "file" 2>&1 sequence saves stdout and redirects stderr into the same file. popd restores the original directory.

Common pitfalls and checks:

  • If py is not present, call the interpreter by full path (for example C:\Python39\python.exe) or ensure Python is on PATH.
  • Writing to protected locations (Program Files, root of C:) can fail silently; choose a writable folder.
  • If the script is launched via file association (double‑click), Windows might use pythonw.exe (no console) or set a different working directory; explicitly invoking the interpreter from the batch avoids that.
  • Hidden extensions in Explorer can make an output like op.txt appear missing if it is actually op.txt.txt.

This pattern is concise and robust for saving a script’s output into a text file while preserving error messages for troubleshooting.

Recommended Answers

All 2 Replies

You could add these two lines before invoking python

echo %CD%
pause

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.py > App1_OUTPUT.txt
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.