Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:08:10+00:00 2026-05-26T05:08:10+00:00

I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +

  • 0

I have a script a.py :

#!/usr/bin/env python

def foo(arg1, arg2):
    return int(arg1) + int(arg2)

if __name__ == "__main__":
   import sys
   print foo(sys.argv[1], sys.argv[2])`

I now want to make a script that can run the first script and write the output of a.py to a file with some arguments as well. I want to make the automate_output(src,arglist) generate some kind of an output that I can write to the outfile :

import sys

def automate_output(src,  arglist):
    return ""


def print_to_file (src, outfile, arglist):
    print "printing to file %s" %(outfile)
    out = open(outfile, 'w')
    s = open(src, 'r')

    for line in s:
        out.write(line)
    s.close()

    out.write(" \"\"\"\n Run time example: \n") 
    out.write(automate(src, arglist))
    out.write(" \"\"\"\n")
    out.close()


try: 
    src = sys.argv[1]
    outfile = sys.argv[2]
    arglist = sys.argv[3:]
    automate(src, arglist)
    print_to_file(src,outfile,arglist)  
except:
    print "error"
    #print "usage : python automate_runtime.py scriptname outfile args"

I have tried searching around, but so far I do not understand how to pass arguments by using os.system with arguments. I have also tried doing :

import a
a.main()

There I get a NameError: name ‘main’ is not defined

Update :
I researched some more and found subprocess and I’m quite close to cracking it now it seems.
The following code does work, but I would like to pass args instead of manually passing ‘2’ and ‘3’
src = ‘bar.py’
args = (‘2’ , ‘3’)
proc = subprocess.Popen([‘python’, src, ‘2’ , ‘3’], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print proc.communicate()[0]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:08:11+00:00Added an answer on May 26, 2026 at 5:08 am

    This is not a function, it’s an if statement:

    if __name__ == "__main__":
        ...
    

    If you want a main function, define one:

    import sys
    
    def main():
       print foo(sys.argv[1], sys.argv[2])`
    

    Then just call it if you need to:

    if __name__ == "__main__":
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the code below: #!/usr/bin/env python from PyQt4 import QtCore, QtGui import os,sys class
I have the following script: #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' Net::SSH.start('host1', 'root', :password
I have following script: #!/usr/bin/python while True: x = raw_input() print x[::-1] I am
I have perl scripts starting with #!/usr/bin/perl or #!/usr/bin/env perl First, what does the
Cron installation is vixie-cron /etc/cron.daily/rmspam.cron #!/bin/bash /usr/bin/rm /home/user/Maildir/.SPAM/cur/*; I Have this simple bash script
I have a python script that is constantly grabbing data from Twitter and writing
Greetings. I have written a little python script that calls MySQL in a subprocess.
anyone have script or procedures to install SQL Server 2008 Express, set up the
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script that retrieves objects from a remote server through an Ajax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.