I want to give command line arguments to a file and then executive that python file from within my master python file. How should I go about this?

I don't really care about the standard output. The file that I want to executive from with my file just outputs all its things to another output file. Therefore, all I want to find out is how to executive a python file (with command line arguments given) from within another python file?

Thanks.

Recommended Answers

All 2 Replies

try running files using terminal/cmd commands:

import os
os.system("file.py arg1 arg2 arg3")

also, if you wanted to run a python file without command line arguments, (don't quote me on this but im pretty sure you can't use CMAs this way) try this:

#import the file
import myfile

# run a function from myfile
myfile.function()

# or if you dont want to put 'myfile' in front of all your variables/functions simply import all of the code using the asterisk symbol *:
from myfile import *

# now we dont have to type myfile.function()
function()

hope that helps :)

you could also use something like exec()
it would be very inpractical though. os.sys() or os.popen() would be your best option.

to use exec() though could look something like:

f = open('name.py','r')
fr = f.readlines()
for i in fr:
  exec(i)

but that would be too much and too complicated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.