I wanted to do it like running an AtCoder test case.

What I wanted to do

Have you ever tried to take advantage of features like AtCoder's code testing and yukicoder's online execution? I wish I could run it in Python.

Well, in short, I wish I could read the .txt file with Python's input ().

This execution environment is Windows 10.

At the command prompt

First of all, let's create an appropriate folder and insert the input.txt file for input and the main.py file containing the input () function into it.

If you move to the folder you created earlier at the command prompt and enter python main.py <input.txt, it's strange that you can read from the text file! !!

By the way, it works even if you do this with open (0) .read ().

In python

I tried to do it with subprocess and killed it for 3 days after trial and error. This is Aho. In the following source code, the output that comes out with the print function comes out. The timeout option has a run time limit, shell = True and it works at a real command prompt. By the way, output comes out in byte type, so if you want to make it a character string type, please boil it with encode () or bake it ...

However, official documentation recommends that you do not use it as it can be a security vulnerability. Winodws If you specify the spell of destruction, not only Python but also Windows will disappear ...

import subprocess
print(subprocess.check_output('python main.py < input.txt',timeout=5,shell=True))

Actually, besides ...

It seems that you can do more in detail with the Popen () function. I don't know at all. It will take about 5 days to understand.

Finally

It became a shit article. I will write more when I get more detailed. .. ..

Recommended Posts

I wanted to do it like running an AtCoder test case.
I wanted to do something like an Elixir pipe in Python
[Machine learning] I tried to do something like passing an image
[Python] What I did to do Unit Test
I want to do something like sort uniq in Python
I made my own OSS because I wanted to contribute to it
I wanted to calculate an array with Sympy's subs method
[Django] I wanted to test when POSTing a large file [TDD]
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
Hash chain I wanted to avoid (2)
I wanted to evolve cGAN to ACGAN
I want to do ○○ with Pandas
Hash chain I wanted to avoid (1)
I wanted to generate a sentence like "Fucking Deca Rashomon" (past form)
I want to do it with Python lambda Django, but I will stop
I made an AI to judge whether it is alcohol or not!
I installed PySide2, but pyside2-uic didn't work, so I managed to do it.
It's more recent, but I wanted to do BMI calculation with python.