0

I guess I have to use either use os.system or subprocess.call, but I can't figure out how to use it.

I don't have the permission to edit the original folder.

subprocess.Popen('file.py', cwd=dirName) gives me 'The system cannot find the file specified' even though the file clearly exists

If I was typing in cmd,

cd directory
file.py -arg

Edit: Just to be clear I want to run another script using a python script

5
  • Look for a docopt package Commented Aug 17, 2018 at 15:01
  • Not sure what you am I looking for in docopt package, I am really sorry, could you please write the code. Commented Aug 17, 2018 at 15:12
  • Visit docopt.org website. It's a python package designed specifically for -arg while running python scripts from cmd Commented Aug 17, 2018 at 15:27
  • No, I want to run a python script from another script, docopt is for making argparse easier, that is not my question Commented Aug 17, 2018 at 15:35
  • @Sergei. docopt has absolutely nothing to do with this question. Commented Aug 17, 2018 at 15:36

3 Answers 3

1

As you have tagged the question with cmd, I assume that you use Windows. Windows is kind enough to automatically use the appropriate command when you type a document name in cmd, but Python subprocess is not. So you have 2 possible ways here

  1. use shell=True to ask a cmd interpretor to execute the command:

    subprocess.Popen('file.py', cwd=dirName, shell=True)
    
  2. pass explicitely the path of the Python interpretor (or the name if it is in the path)

    subprocess.Popen([python_path, 'file.py'], cwd=dirName, shell=True)
    
Sign up to request clarification or add additional context in comments.

Comments

0

At first you need to add the python in windows environment. Then you can add the file path (the file that you want to run it on command line). Then you can go to command line page and type the file name and use it like the line below:

FileName commands

for example :

pip install datetime

Comments

0

Its clear you are probably using python 3 rather than python 2. Otherwise you might use os.system as already suggested or commands. Commands is now obsolete as of python 3. To get this working I would use instead:

statusAndOutputText = subprocess.getstatusoutput( os.path.join( dirName, 'file.py' ) )

This will definitely work. I've used it many times in python 3 and it will give you the status in statusAndOutputText[0] and output buffer in statusAndOutputText[1] which are both very useful to have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.