TLDR: Add the following to your launch.json
file and the VSCode debugger will pass data to stdin from a file called input
located in your workspace folder.
"args": ["<", "${workspaceFolder}/input"],
I recently started trying out Google’s Kick Start competition to hone my skills. One of the things that initially confused me was how inputs to a problem were read. Unlike websites like HackerRank or LeetCode, Kick Start requires that you read the input from stdin and write your output to stdout (see https://codingcompetitions.withgoogle.com/kickstart/faq under the Coding section).
Unfortunately, manually typing in input cases didn’t appeal to me and I wanted to automate the process by reading the input from a separate file. One of the methods I initially used was angle brackets:
python3 solution.py < input > output
This worked but I had to run the file from a terminal and I couldn’t use the debugger. I eventually found a solution on StackOverflow. You just add the args
parameter to your launch.json
file and put in the arguments you would normally use in your terminal:
"args": ["<", "${workspaceFolder}/input"],
This will make VSCode read a file called input
located in your workspace folder and pass it to stdin. In my case, I wanted to read from a file located in the same folder as my script, and I also wanted to save the output to a separate file, so I used this:
"args": ["<", "${workspaceFolder}/${relativeFileDirname}/input", ">", "${workspaceFolder}/${relativeFileDirname}/output"],
I hope this helps!