1

I am well aware that there are multiple threads dealing with this problem because I used them in an attempt to self-teach me how to do it. However, for me it does not work and I wondered if someone could help me find my error.

So I got one program (let's call it prog1) that calls a script for every string-variable like this:

os.system(r'C:\Users\user\docs\bla\script.py %s'%variable)

In script.py I now just wanted to test if this call worked by disabling all code but just doing:

def main(string):
    print(string)
    root

if __name__ == "__main__":
    print('I got executed')
    main(sys.argv[1])

The Problem is, if I execute prog1 nothing happens. No errer - so it runs through and the console pops up for a Brief second. However, nothing happens - no string is printed. If I execute script.py directly it works though.

EDIT: I got it to execute without error with the subprocess package by using:

subprocess.call(r'C:\...\script.py' %s' %variable,shell = True)

However, the problem stays the same - nothing happens. Or well, I noticed now that in the Background a Windows window pops up asking me with which program I want to execute files with the ending '.py' but this is not helping me, because I want to execute in console :(

1
  • 3
    You should use the subprocess module, it allows you to pass on arguments. Commented Jul 16, 2018 at 12:00

1 Answer 1

1

Try using the subprocess module, it allows for more flexible parametres and other configuration changes :

import subprocess
subprocess.check_call(["python.exe", r"C:\Users\user\docs\bla\script.py", variable])

And when executed :

subprocess.check_call(["python", r"C:\Users\user\docs\bla\script.py", "Blue"])

I got executed

Blue

More information about the subprocess : https://docs.python.org/2/library/subprocess.html

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

7 Comments

Thank you for the quick Response, however this results now in the following error: OSError: [WinError 193] %1 is not a valid Win32 application
My bad, went too fast. You need to add python as your first argument to your subprocess call.
ok, I really feel stupid now, but this does not execute as well for me, it gives the following error: TypeError: bufsize must be an integer
my bad, I forgot the brackets [...] - with this brackets it does not give an error anymore but it is stuck in an endless loop - it is never terminating - I think I nearly got every possible error there is by now xD
I executed your exact code except for the root part. I don't know what meaning it holds without more context but I bet that causes your loop.
|

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.