4 Replies - 794 Views - Last Post: 07 June 2016 - 01:22 PM Rate Topic: -----

#1 castro   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

call python script in another by passing variables

Posted 07 June 2016 - 12:29 PM

I am writing a python script on ubuntu and i want to call a script with another script by passing variable.

script1.py calls script2.py

import sys,os,glob
count = 20
dir2="path/file*"
try: 
 files=glob.glob(dir2)
 for name in files:
       os.system('python script2.py name[9:] count "OUTPUT CREATES A FILE NAME LIKE final.xml"



The above code does not work it gives me an error syntax error at the os.system( code for all that line


but when i lunch the above command on terminal as below it works

terminal

> python script2.py file_2_1 20 FinalFile.xml

the script2.py accept input variables of two and generate file of your type example FinalFile.xml if you specify on terminal

Is This A Good Question/Topic? 0
  • +

Replies To: call python script in another by passing variables

#2 DblAAssassin   User is offline

  • D.I.C Regular

Reputation: 43
  • View blog
  • Posts: 322
  • Joined: 11-May 13

Re: call python script in another by passing variables

Posted 07 June 2016 - 12:39 PM

Could it have anything to do with the unclosed quotes and parentheses in that line?
Was This Post Helpful? 0
  • +
  • -

#3 castro   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: call python script in another by passing variables

Posted 07 June 2016 - 12:41 PM

I thought of the same. I checked all but none..
is there another way of doing this
Was This Post Helpful? 0
  • +
  • -

#4 DblAAssassin   User is offline

  • D.I.C Regular

Reputation: 43
  • View blog
  • Posts: 322
  • Joined: 11-May 13

Re: call python script in another by passing variables

Posted 07 June 2016 - 12:48 PM

Have you checked out this module? If you are using Python 3.X, this module was intended to replace os.system and os.spawn*

https://docs.python....subprocess.html

EDIT:
This interface is what your looking for.
https://docs.python....ubprocess.Popen

This post has been edited by DblAAssassin: 07 June 2016 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 castro   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 12-November 05

Re: call python script in another by passing variables

Posted 07 June 2016 - 01:22 PM

Thanks I figured it out i concatenated them variables as follows

os.system('python script2.py + " " + name[9:] + " " + count + " " + "OUTPUT CREATES A FILE NAME LIKE final.xml"

and it worked...
thanks for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1