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.

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
Python Newbie
  • 45
  • 1
  • 7
  • Are you do really need to run scripts in such way? If so, read the documetation of [exec](https://docs.python.org/3/library/functions.html#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](https://docs.python.org/3/library/subprocess.html). Also check out this question: https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another – Aleksandr S Sep 05 '18 at 07:49

1 Answers1

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.

Devanshu Misra
  • 773
  • 1
  • 9
  • 28
  • 1
    Just as a note: the [documentation of `os.system`](https://docs.python.org/3/library/os.html#os.system) discourages the use of this function in favour of the [subprocess](https://docs.python.org/3/library/subprocess.html) module. Specifically, [`subprocess.call`](https://docs.python.org/3/library/subprocess.html#subprocess.call) is [recommended as a replacement for `os.system`](https://docs.python.org/3/library/subprocess.html#replacing-os-system) – shmee Sep 05 '18 at 08:13
  • 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. – Devanshu Misra Sep 05 '18 at 08:16
  • 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 Sep 05 '18 at 08:37
  • @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. – Devanshu Misra Sep 05 '18 at 09:16
  • os.system commands runs into error with the import statement within my python script. ./script.py: line 2: import: command not found ./script.py: line 3: from: command not found – Python Newbie Sep 05 '18 at 09:16
  • Also, please make edits to your question to make it visible. Commenting things wont make it easier for us to read – Devanshu Misra Sep 05 '18 at 09:19
  • @DevanshuMisra, I am not sure how to add the new line, the comment is well framed but as soon as I finish editing it puts everything into one line. – Python Newbie Sep 05 '18 at 09:22
  • Yes I understand. Please check the edits I made to my answer. You might have to specify `python` version and the full directory path to your script in order to run it. Also, which Python version are you using? Because the code i wrote for you is in Python3+ whereas seeing your traceback call, I assume you are using Python2 and hence the syntax error/command not found – Devanshu Misra Sep 05 '18 at 09:27
  • Python version 2.7.14. Content of the script.py file is below two lines import sys print sys.argv[1] When I execute it using os.system I get following error. >>> os.system('./script.py hi') ./script.py: line 1: import: command not found ./script.py: line 3: print: command not found – Python Newbie Sep 05 '18 at 09:32
  • Did you import `os` in the other file where you are using os.system?? Also, send me a copy of both of your files on devanshumisra95@gmail.com. It'd be better if i see for myself. Ping/Comment here once you do – Devanshu Misra Sep 05 '18 at 09:44
  • 1
    @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](https://stackoverflow.com/a/19305076/4134674) as the first line of your `script.py` file (assuming from the path you are on a Unix/Linux/Mac platform) – shmee Sep 05 '18 at 11:47
  • @shmee.. thanks for pointing the error.Thanks for all your help Devanshu. – Python Newbie Sep 05 '18 at 13:12