I am using Visual Studio Code to debug my Python code.
I want to redirect one text file in stdin
and the output be written in another file.
I can reach that by running python directly using the following syntax:
python code.py < input.txt > output.txt
Is there a way to allow that while debugging the script? If that is not possible, can I create a configuration for running python with that parameters. I tried using args
parameter in the launch.json
, but those are all placed into quotes that defeats the purpose of this redirection.
If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.
Just click the Run Python File in Terminal play button in the top-right side of the editor. Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.
I was able to achieve this using the args parameter in the launch.json config file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [ "<", "input.txt",
">", "output.txt" ]
}
]
}
When running the debugger you get the following command line:
bash-3.2$ env DEBUGPY_LAUNCHER_PORT=51669 /usr/local/opt/python/bin/python3.7 /Users/XXXX/.vscode/extensions/ms-python.python-2020.4.76186/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher "/Users/XXXX/my_script.py" < input.txt > output.txt
Visual Studio Code 1.45
macOS 10.14.6
There isn't any built-in solution, but if you modified your app to replace sys.stdin
and sys.stdout
with what you would like to be considered stdin
and stdout
you could get the same effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With