I have two Python scripts, a parent script (parent.py) which passes a value to the child script through subprocess, and a child script (child.py), which uses the argument given in subprocess to return a value to the parent function. Some background: in my actual script, I read in PDF files in the parents and subprocess the child whenever the PDF is not readable. The child then OCRs the PDF (I pass the PDF path from the parent to the child through subprocess) and is supposed to return the text to the parent (this is what currently fails).
This is the code that I have, I use Python 3.9 on Windows.
parent.py
import subprocess
def main():
a='The dog jumps. Over a fence.'
subprocess.call(["python", "child.py", a])
main()
from child import test
child_result = test()
print(child_result)
child.py
import sys
def main():
a = sys.argv[1].split('.')
return a
def test():
for i in main():
output = i
print(output)
if __name__ == '__main__':
main()
I would like to use the variable output from the child function in the parent function, but when I try to run the code I get this error message:
Traceback (most recent call last):
File "P:\2020\14\Kodning\Code\custodyproject\parent.py", line 10, in <module>
child_result = test()
File "P:\2020\14\Kodning\Code\custodyproject\child.py", line 8, in test
for i in main():
File "P:\2020\14\Kodning\Code\custodyproject\child.py", line 4, in main
a = sys.argv[1].split('.')
IndexError: list index out of range
The error seems to stem from including sys.argv in the function test(); when I return just a random variable from test() to the parent it works. My question is: can I return variables from the child to the parent that are based on the subprocess arguments given in the parent?
Thank you for your help!