2

I have old python. So can't use subprocess. I have two python scripts. One primary.py and another secondary.py. While running primary.py I need to run secondary.py.

Format to run secondary.py is 'python secondary.py Argument'

os.system('python secondary.py Argument')...is giving error saying that can't open file 'Argument': [Errno 2] No such file or directory

2
  • You can use the subprocess backport. Commented May 1, 2013 at 20:55
  • You mention in a comment below that you are using python 2.6. Subprocess is available for that version of python. Commented May 1, 2013 at 21:22

2 Answers 2

1

Given the code you described, this error can come up for three reasons:

  • python isn't on your PATH, or
  • secondary.py isn't in your current working directory.
  • Argument isn't in your current working directory.

From your edited question, it sounds like it's the last of the three, meaning the problem likely has nothing to do with system at all… but let's see how to solve all three anyway.

First, you want a path to the same python that's running primary.py, which is what sys.executable is for.

And then you want a path to secondary.py. Unfortunately, for this one, there is no way (in Python 2.3) that's guaranteed to work… but on many POSIX systems, in many situations, sys.argv\[0\] will be an absolute path to primary.py, so you can just use dirname and join out of os.path to convert that into an absolute path to secondary.py.

And then, assuming Argument is in the script directory, do the same thing for that:

my_dir = os.path.dirname(sys.argv[0])
os.system('%s %s %s' % (sys.executable, 
                        os.path.join(my_dir, 'secondary.py'),
                        os.path.join(my_dir, 'Argument')))
0

Which python version do you have? Could you show contents of your secondary.py ? For newer version it seems to work correctly:

ddzialak@ubuntu:$ cat f.py 
import os
os.system("python s.py Arg")

ddzialak@ubuntu:$ cat s.py

print "OK!!!"
ddzialak@ubuntu:$ python f.py
OK!!!
ddzialak@ubuntu:$ 
4
  • Just because it works in one scenario doesn't mean it works in every possible scenario. Your code will not work if, e.g., python isn't on the PATH, and you do /opt/python23/bin/python f.py at the shell.
    – abarnert
    Commented May 1, 2013 at 20:59
  • I should have been more clear. Both files primary.py and secondary.py are in the same directory.....I have python 2.6.2......I need to run secondary.py Argument at the shell....secondary.py basically searches for files with name "Argument" in the current directory and prints them....It works fin when I run secondary.py independently....only when I try to do it through primary.py that I am getting the error.....Thanks a lot again Commented May 1, 2013 at 21:03
  • Yes, I know very well that example is not a proof. Anyway for me it's very unexpected to have message like: can't open file 'Argument' - if there would be some problem with PATH then message will say that python or secondary.py could not be opened.
    – ddzialak
    Commented May 1, 2013 at 21:06
  • If you have python 2.6.2 then subprocess should be available (since 2.4)
    – ddzialak
    Commented May 1, 2013 at 21:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.