Pro Girl
Pro Girl

Reputation: 952

How to call script with python in the same directory with subprocesses?

I'm trying to open a script from the same directory with subprocesses with python 3 in Windows 10,(I Am the administrator) and using pycharm, however I'm getting the following errors for any alternative solution I try:

Here is my code:

import subprocess
subprocess.call(['C:\\Users\\CobraCommander\\PycharmProjects\\BlackBox', 'Avalon.py'])  # The above "BlackBox" it's the directory for both files.

with this I get the following error:

PermissionError: [WinError 5] Access is denied

If I try instead:

subprocess.call(['python Avalon.py'])

with this I get the following error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

so I tried:

subprocess.call(['C:\\Users\\CobraCommander\\PycharmProjects\\BlackBox\\Avalon.py'])

with this I get the following error:

OSError: [WinError 193] %1 is not a valid Win32 application

I also tried to run as administrator from terminal and get same error:

PermissionError: [WinError 5] Access is denied

Kindly before trying to mark as duplicate, note that I have already read the other posts for the errors as well as subprocesses.

Can anybody advise how to lunch this script in python from another script?

Upvotes: 0

Views: 617

Answers (2)

suitsense
suitsense

Reputation: 48

Add python before your script so instead of

subprocess.call(['C:\\Users\\CobraCommander\\PycharmProjects\\BlackBox\\Avalon.py'])

use

subprocess.call(['python', 'C:\\Users\\CobraCommander\\PycharmProjects\\BlackBox\\Avalon.py'])

Make sure your PYTHONPATH environment variable is set.

Upvotes: 2

abdusco
abdusco

Reputation: 11061

You need to supply cwd argument to set the working directory:

https://docs.python.org/3/library/subprocess.html#subprocess.call

import subprocess

if __name__ == '__main__':
    subprocess.run(r'touch d:\test.txt')
    p = subprocess.run(r'ls -la test.txt', cwd=r'd:\\', stdout=subprocess.PIPE)
    print(p.stdout.decode())

output:

-rw-r--r-- 1 abdusco 197609 0 Jul 18 13:32 test.txt

Upvotes: 1

Related Questions