Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Command line argument to Python program using IDLE? [duplicate]

I have downloaded a python file xxxxxx.py that is supposed to run on the command line by typing: python xxxxxx.py filename1 filename2 and that should take these two files as arguments.

I was wondering if there is a way I can use IDLE to pass in these arguments. Is there a way other than setting sys.argv ?

Thanks

like image 428
Saher Ahwal Avatar asked Feb 02 '11 00:02

Saher Ahwal


2 Answers

It depends on the content of your Python file. If it is well-written, like:

#! /usr/bin/env python

def process(files):
   for file in files:
       # ...

if __name__ == '__main__'
    # some error checking on sys.argv
    process(sys.argv[1:])
    sys.exit(0)

Then you could simply import the python file and run it like:

 import name_of_file

 # ...
       name_of_file.process([file1, file2, file3])
 # ...

So, it really depends on how it is written. If it isn't written well but you can edit it, I would refactor it so that it can be used as a library; otherwise, I would use the subprocess module to invoke the program.

like image 176
Michael Aaron Safyan Avatar answered Sep 24 '22 02:09

Michael Aaron Safyan


You can do this from the command line with:

idle.py -r scriptname.py put arguments here

You can try a different IDE like ActivePython

Or you can patch IDLE:

http://bugs.python.org/issue5680

like image 25
cjones26 Avatar answered Sep 26 '22 02:09

cjones26