-2

I am a beginner to Python, and I would like a way to run a Python file from Python.

For example, I have a Python file named checker.py. In that file, I would like to iterate over a folder that contains inputs and outputs, and I would like to, using the Python script, give input to a different Python file, and check if it matches the expected output (in a different file). Is there any way to do this in Python?

Here is the GitHub link for the problems I have completed and need to check so far: https://github.com/vishnupsatish/CCC-practice

Vishnu S.
  • 141
  • 1
  • 2
  • 8
  • Can you give a small example of what you're looking to accomplish or any code you've written? – C.Nivs Jan 26 '20 at 18:39
  • What is a folder with input and output? – Jürgen Gmach Jan 26 '20 at 18:40
  • @C.Nivs Yes. Here is the GitHub link. https://github.com/vishnupsatish/CCC-practice. In all of the year folders, I need to add input and output folders containing all of the input and output files. Using the checker.py (which is currently blank), I will go through all of the years and problem folders, and run each Python file one-by-one. Then, I will give it input using the input file, and check if the output matches the output file – Vishnu S. Jan 26 '20 at 18:47
  • @J.G. The input and output files are multiple .in and .out files that I will need to check the problems for. It is programming practice, and I need to solve problems and instead of manually checking if the input and output are correct, it would be quicker to create a Python script that achieves that. – Vishnu S. Jan 26 '20 at 18:49
  • 1
    This seems to be an [XY problem](http://xyproblem.info/). The sensible way to run Python code from one file in another Python script is to `import` it, not run it as a subprocess. Design your `checker.py` file to be a module with a `do_checks()` function which can be imported into other scripts, and if you want to run `checker.py` from the commandline, write `if __name__ == '__main__': do_checks()` at the end of it. See e.g. https://stackoverflow.com/questions/419163/what-does-if-name-main-do/ – kaya3 Jan 26 '20 at 19:00

1 Answers1

0

Try to split your big problem into several smaller ones.

First, try to find all files.

Then execute the Python files ( https://docs.python.org/3/library/subprocess.html )

Then read the output files and compare the results.

As this is a practice for you, I won't deliver all the code. Just try to do one small task after the other. And then ask when you encounter a problem.

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37