1

I am trying to run a python script from another python script but i am getting blocked because there is a space in the passed argument. The script I am trying to run is ran from the command terminal with the name and the arguments as such

>>>Duplicate_Checki.py "Google Control Center" "7.5 Hardening"

In the script which i try to call the first script the code looks like this:

def run_duplicate_check(self):
    os.system("python Duplicate_Checki.py Google Control Center 7.5 Hardening") 

I get the following error

Duplicate_Checki.py: error: unrecognized arguments: Center 7.5 Hardening

Also tried os.system("python Duplicate_Checki.py {} {}".format("Google Control Center" ,"7.5 Hardening")) with the same error

I have also tried

os.system(python Duplicate_Checki.py "Google Control Center" "7.5 Hardening")

but i get invalid syntax

2
  • You'll need to escape the spaces: os.system('python Duplicate_Checki.py "Google\ Control\ Center 7.5\ Hardening"') Commented Jun 20, 2017 at 21:23
  • 1
    Look at subprocess.run. It doesn't do any splitting on spaces. Commented Jun 20, 2017 at 21:24

1 Answer 1

2

script.py:

import sys

if __name__ == '__main__':
    args = sys.argv[1:]
    print(args[0])
    print(args[1])

runner.py:

from subprocess import call

call(["python3", "script.py", "Google Control Center", "7.5 Hardening"])

Execution python3 runner.py, output:

Google Control Center
7.5 Hardening

See https://docs.python.org/3/library/subprocess.html

#subprocess.run, #subprocess.check_output, #subprocess.call

Sign up to request clarification or add additional context in comments.

1 Comment

I'am running the version Python 2.7, so i used call(["Duplicate_Checki.py", "Google Control Center", "7.5 Hardening"], shell = True)

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.