0

I am having issues figuring out how I can go up a directory from my server running to run a python script. I am able to run a python script using the child process, {spawn}, however, I am unable to go up once, to the parent directory to run the python script.

The code I currently have works only if the file is in the current direcrtory

    const b = brand+'.py'

    const childPython = spawn('python3', [b]);

    childPython.stdout.on('data', (data) =>{
        console.log(`data: ${data}`)
    })

1 Answers1

1

I presume you're using child_process.spawn() to run your python script. To go up one directory level from where your script is, you can just pass the cwd (current working directory) option to child_process.spawn().

child_process.spawn(command, args, {
    cwd: path.resolve(__dirname, "..")
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I am using this const childPython = spawn('python3', [b]); where p is the filename. Should I run the code you provided before that? I tried putting the 'python3' and [b] instead of command and args but it doesnt work – Antonio Chelala Feb 17 '22 at 01:11
  • @AntonioChelala - I'm suggesting an option you pass to your existing `child_process.spawn()`. A visit to the `spawn()` documentation and reading the various options will help. If you added your ACTUAL code to your question, we could help more specifically. You will ALWAYS get faster and better answers here at stackoverflow if you include your actual code. Theoretical questions get theoretical answers. Specific questions with actual code get specific answers with actual code. – jfriend00 Feb 17 '22 at 01:13
  • Thanks for the recommendations and the tip :) Added explanation code above – Antonio Chelala Feb 17 '22 at 03:31