I have a Python script named sample.py containing multiple functions.
def A():
return 3
def B():
return [i for i in range(2,10)]
I have to write a Makefile which calls the function B from sample.py and stores its output to another file (say output.txt); i.e. I wish to run make output.txt from the command line and store the result of calling function B from sample.py. How do I write the recipe for it?
We can call a script directly from the Makefile, but how do I call a particular function?
output.txt:sample.py
___recipe___
python -c <cmd> allows you to pass a string of Python code to the interpreter. So we just need to write a one-liner script (semicolons do work in Python; they just normally aren't necessary and are discouraged) that imports the .py file and calls the function. Something like:
output.txt: sample.py
python -c "import sample; print(sample.B())" > output.txt
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