0

im new to subprocessing and I have a question. I can't find a proper solution online.

I want my path to be in a variable --> that will be passed to a function --> that will be passed to a subprocess.
I'm not allowed to show my real code, but this simple example (that I just can't get to work) would help me a lot.
This code snippet should do:

  1. Just take my path from a variable.
  2. "cd" the path in CMD.
  3. Open a file that is located in this path.

So far I tried:

import subprocess 

test_path = "C:/randome_path/.."

def Test_Function(test_path):         
    subprocess.call("cd", test_path, shell = True)                                                                                                                                                          
    subprocess.call("python file.py", shell = True)
    
Test_Function() 

My ErrorMessage is:
TypeError: Test_Function() missing 1 required positional argument: 'test_path'

Thank you for your time!

  • 1
    Running Python as a subprocess of itself is often something you want to avoid anyway. Either `import` the code you want to run, and run it as part of the current process, or use `multiprocessing` to run it in a separate process under the control of the current script. – tripleee Jun 15 '22 at 10:22

1 Answers1

0

First you need to pass a parameter to your function because that's how you declarerd:

Test_Function(test_path) # here the function call with parameter 

or using the key-value "approach"

another_path = # ...
Test_Function(test_path=another_path)

Second: the command is expecting a string not a further parameter

subprocess.call(f"python file.py", shell=True, cwd=test_path) 

Note 1 to execute such command python file.py it is assumed that python's path is declared in some environment variable

Note 2 that subprocess may have some different "behaviour" under Windows

Try without shell=True. Now the commands should be given as a list of strings

def Test_Function(test_path):         
    subprocess.call(["python", "file.py"], cwd=test_path)
cards
  • 3,936
  • 1
  • 7
  • 25
  • Thank your for the fast response. Sadly I get the same ErrorMessage: "TypeError: Test_Function() missing 1 required positional argument: 'test_path'" – Senior Banan Jun 15 '22 at 09:35
  • @Senior Banan have you deleted this cmd `Test_Function()`? You should run `Test_Function(test_path)` instead – cards Jun 15 '22 at 09:37
  • You're completly right. I forgot it. I tired it again, but it seems that the code is still not doing what it's supposed to do. Right now I think it subprocesses the "cd" command -> closes everything after "cd" -> then tries to open the next command with a new shell (with no previous "cd" command in it). After that I tried to have everything in one line `subprocess.call(f"cd {test_path}", "py file.py", shell=True)`, but this gives me a whole new set of Errors. – Senior Banan Jun 15 '22 at 10:03
  • @Senior Banan add the errors please. Maybe you should not change directory in that way... but with `os.chdir(test_path)` and then execeute the python script – cards Jun 15 '22 at 10:09
  • Notice the under Windows `suprocess` could behave differently see [doc](https://docs.python.org/3.9/library/subprocess.html) – cards Jun 15 '22 at 10:14
  • @Senior Banan my answer is about using `subprocess` but for this case, dealing with python's files, you should consider the suggestion of _tripleee_ in the comment – cards Jun 15 '22 at 10:29
  • Thank you again for helping me out. I tried everything out in a normal enviroment and both `subprocess.call("python file.py", shell=True)` and `subprocess.call(["python", "file.py"])` worked together with `os.chdir(test_path)`. On an .ssh enviroement only `subprocess.call(["python", "file.py"])` seems to work and the other command not. That confuses me a little bit. The last ErrorMessage I have is : `can't open file 'file.py': [Errno 2] No such file or directory`. Is there a special way of passing paths to remote hosts? – Senior Banan Jun 15 '22 at 11:28
  • (I'm sorry for not mentionioning it earlier, it didn't seem to be relevant) – Senior Banan Jun 15 '22 at 11:28
  • could u explain me your last comment? This is my first post here, I think I did not quiet understand it. Sorry for that. – Senior Banan Jun 15 '22 at 11:34
  • if it `can't open file...` it is because the program does not find the file because it is in wrong directory, see the link at the top of the page "This question already has answers here". Also reedited my answer – cards Jun 15 '22 at 11:42