1

I am newbie in python programming and need your help. I am running a python script using exec(open("./path to script/script.py").read()). If I try to pass a argument then I always get the error the file doesnt exists, somehow the interpreter assumes that the string passed is the file name which is obviously not correct.

>>> exec(open("./path to script/script.py"  "hello").read())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: './path to script/script.pyhello'

Anybody has any tip on how to resolve this.

Thanks for your help.

1
  • Are you do really need to run scripts in such way? If so, read the documetation of exec. exec function is for running python code represented as a string not a file. With exec you can "pass" parameters to you code with globals and locals but it is not command line arguments. I suggest you to try subprocess. Also check out this question: stackoverflow.com/questions/7974849/… Commented Sep 5, 2018 at 7:49

1 Answer 1

1

Since you want to open a file and pass an argument as well, I'd advise you to use os.system() from the os module to achieve this as exec() does not provide you with that kind of functionality as you can only pass a string into it. Example:

Script.py:

arg = input()
print(arg)

Call to the above Script:

import os

os.system('python3 home/pathtoscript/script.py Hello')

This will print the desired output for you on the terminal. But it also depends on what you want to achieve further with it.

Output:

Hello

I'd also suggest using argparse module for dealing with arguments as it provides great support if you are trying your hand at multiple arguments. Here is the official documentation for the same.

Good Luck and Happy Coding.

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

12 Comments

Just as a note: the documentation of os.system discourages the use of this function in favour of the subprocess module. Specifically, subprocess.call is recommended as a replacement for os.system
Sure does. Subprocess is the thing to go for. But for a newbie, I'd suggest os.system as it is a simple call to something that pritns on the terminal. I majorly suggest subprocess if the code is complex and expects a return from the script to be executed. I know I can be wrong, but I just wanted him to have a simple way to go through this and not get discouraged while using subprocess, as understanding how it works sure is a tough job.
I kind of disagree :) The fact that os.system only takes one argument does not make it simpler. You can use subprocess.run with a single string argument as well. In fact, os.system becomes more complicated once you go beyond simply fire & forget one command. But it's your answer, so you do you. If any interested reader decides to opt for os.system its fine by me, I just wanted to give them the option to at least consider subprocess.run or subprocess.call, hence my comment :)
@shmee Sir, you are absolutely correct. I'm just trying to help with the little knowledge I have, and this was the first answer that came to my mind. I'm subprocess has better calls and handles these scenarios better.
@PythonNewbie This looks like your system has no clue which interpreter to use for your script. Either use python /path/to/script.py (or python2 or python3) in your os.system, as suggested by @DevanshuMisra in the answer, or make the script executable and add a shebang line as the first line of your script.py file (assuming from the path you are on a Unix/Linux/Mac platform)
|

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.