0

I am trying to run a python script within another python script. Which will run 10 times and produce 10 outputs.

I want to run program1.py inside program2.py. Now my program1.py was initially taking a C executable inside it and it takes 1 command line argument.

The program1.py looks like below:

import os
import sys
dataset = sys.argv[1]

os.system(f"/home/Dev/c4.5 -u -f {dataset}")
os.system(f"/home/Dev/c4.5rules -u -f {dataset}")
os.system(f"/home/Dev/c4.5rules -u -f {dataset} > Temp")

f = open('Temp')
# Some code

Where c4.5 and c4.5rules are the name of the executable files. To run this I was using python3 program1.py dataset_name

Now I am trying to put this program1.py inside program2.py and I am trying this below approach:

import os
import subprocess

# Some code
for track in range(0, 10):
    with open(f'Train_{track}', 'r') as firstfile, open(f'DF_{track}.data', 'w') as secondfile:
        for line in firstfile:
            secondfile.write(line)
    os.system("/home/Dev/program1.py DF_track")
    #subprocess.Popen("/home/Dev/program1.py DF_track", shell=True) 

Where I simply want to get the output of program1.py 10 times and want to use DF_track as the command line input for each output generation.

Using above approach I am getting lots of error. Please help.

Edit_1 :

Actually whenever I am trying to run, my cursor is not working, it is freezing, so unable to copy the errors.

Here are some of them :

1. attempt to perform an operation not allowed by security policy.
2. syntax error : word expected (expecting ")")
7
  • "I am getting lots of error": such as? Commented Nov 30, 2021 at 21:00
  • Show the full (or at least a substantial part) of the error(s) as properly formatted text in the question. Commented Nov 30, 2021 at 21:01
  • You need a shebang line at the start of program1.py (#! /usr/bin/python3), and you need to make it executable (chmod +x program1.py). But the much smarter method would be to put the program1.py code into a function, and just import it into program2. It's silly to start a new process for this. Commented Nov 30, 2021 at 21:02
  • I have edited the question and added some of the errors. I have just started to learn python so unable to figure out properly how to approach this. Commented Nov 30, 2021 at 21:09
  • os.system("/home/Dev/program1.py DF_track") why to run python script via system command - why dont you import it and use it? Commented Nov 30, 2021 at 21:09

2 Answers 2

2

Imagine I have 2 files, the first file is a.py and the other is b.py and I want to call the a.py from b.py.

The content of a.py is:

print('this is the a.py file')

and the content of b.py is:


import os

stream = os.popen('python3 a.py')
output = stream.read()

print(output)

Now when I call b.py from terminal I get the output I expect which is a.py print statment


user@mos ~ % python3 b.py
this is the a.py file

You can do this with subprocess too instead of os module.

Here is a nice blog I found online where I got the code from: https://janakiev.com/blog/python-shell-commands/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much .According to my code how am I supposed to use that DF_track as my command line input here?
0

See the example below.

a.py

def do_something():
  pass

b.py

from a import do_something

do_something()

2 Comments

this example lacks detail (needs elaboration). you are simply showing how to create an import in another file and calling that method, what are you achieving here.
Thank you for this. But according to my code can you please guide me saying how can I use that DF_track as my command line input then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.