0

I am trying to run a python script first.py from within another python script second.py.

second.py contains the statement os.system("python first.py").

first.py : default open App was notepad previously. But, I changed Default Program to python.exe and now nothing happens. first.py doesn't even run.

Can anyone help?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
gaurav gurnani
  • 2,789
  • 3
  • 19
  • 18

3 Answers3

1

Instead of using os.system(), a clean way to execute another script is to import it as a module:

import second

The will cause "second.py" to be executed.

>>> with open('test.py', 'w') as f: f.write('print "hello world"')
... 
>>> import test
hello world
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 1
    One thing to be careful of is namespace conditionals. If code in the imported script is prefixed with 'if \_\_name\_\_ == "\_\_main\_\_":' then the code will not run after imported, but will when executed through a call to os.system(). – Alex Jan 13 '15 at 17:37
1

You can run your another python program using subprocess.call module.

import subprocess

subprocess.call("second.py", shell=True)
martineau
  • 119,623
  • 25
  • 170
  • 301
1

If all the scripts are from "trusted" sources, you could safely use execfile().

with open('second.py', 'w') as f: f.write('print "hello world"')

try:
    execfile('second.py')  # -> hello world
except Exception as e:
    # could also be "pass", "time.sleep()", etc
    print 'exception {} occurred'.format(e)  

print 'continuing on...'

One advantage of this is that it's independent of which Default Program is associated with Python scripts. Also the argument to execfile() can be the complete path to a script in another folder, for example:

    execfile('c:/path/to/different/directory/first.py')
martineau
  • 119,623
  • 25
  • 170
  • 301
  • this brings another question.. The code is breaking if first.py has an error. I want my code to ignore the error and proceed with second.py , thrid.py and so on.... can you please help? – gaurav gurnani Jan 15 '15 at 15:48
  • I can try. What kind of error occurs or exception is raised? – martineau Jan 15 '15 at 16:51
  • in these files, I am trying to connect to different FTP servers.. On a few occasions when the server doesn't respond or is unavailable i get error 10061 or 10060.. but if i try later , evrything works fine.. I just don't want other scripts to fail when one of the FTP servers didn't connect. – gaurav gurnani Jan 15 '15 at 17:02
  • OK, I updated my answer. However it would be even better to put a specific exception (or tuple of them), rather than a generic one, `Exception`, shown. – martineau Jan 15 '15 at 17:08
  • Thank you so much..it solved my problem.. I will do some research on exceptions to use a specific one for my case..Thanks again – gaurav gurnani Jan 15 '15 at 18:46
  • You're welcome. I suggested this because I've used it myself since it can give one more control (or do so more easily) than just importing one module into another (as the accepted answer of the question yours was marked a duplicate of does). – martineau Jan 15 '15 at 19:12
  • Hey there, need your help once again... I am getting an FTP exception.. error: 421 user account expired... can u tell me how would i be able to ignore just this one FTP error and catch all other FTP errors... I have done a lot of research online but couldn't find a way.. Thanks in advance – gaurav gurnani Jan 21 '15 at 18:52
  • If your `except` clause specifically catches an FTP exception, you should be able to check the caught exception's `value` attribute to see if it's `421` and just ignore it in that case (or not ignore it depending on the logic within your handler). – martineau Jan 21 '15 at 20:05