0

I have two Python files, file 1 and file 2 that does two separate things. I want to run them together. I am using VS2017

The pseudo code for file 1 is:

Class A:
   foo1():
    .
    .
   foo2();
    if variable<30;
        #do this
    else;
      process = subprocess.Popen('py file2.py' ,shell=True,stdin=None, stdout=None, stderr=None, close_fds=True)

    #rest of the code for foo2()

if __name__ == "__main__":   
  A.foo2();

I also wanted to pass a variable to the second file. I am looking into something like

variable = input("enter variable)
cmd = ['py file2.py', variable]
subprocess.Popen(cmd ,shell=True,stdin=None, stdout=None, stderr=None, close_fds=True)

I am getting an error saying:

'"py subproleap.py"' is not recognized as an internal or external command, operable program or batch file.

And Even if I do pass the variable how do I get the file2.py to read it?

Any help would be appreciated. I am a beginner in python.

EDIT1: Why I use subprocess is that I want the file2.py to run in the background,once the condition is met, while the main process runs in the side.

Abhishek V. Pai
  • 241
  • 1
  • 10
  • 27

1 Answers1

0

This may answer your question (you are using the shell=True param)

But wouldn't it be easier to import file2 as a module in file1? (if you're not already familiar with importing, you can refer to this guide)

Er...
  • 526
  • 4
  • 10
  • Linking to another question is not a good answer; unfortunately, that cancels out the +1 I would give for the `import` suggestion. – tripleee Mar 19 '19 at 15:32