0

I am able to save the cmd data onto a text file using the following command:

python code_3.py > output.txt

However I am calling code_3.py from primary_script.py by writing:

import code_3
os.system('loop3.py')

But I want it to perform the functionality of the what the previous line does. This doesn't work:

os.system('loop3.py > opt.txt ')

Can someone please tell me what to do?

2
  • 1
    You should probably be using the subprocess module, os.system is pretty much deprecated. Commented Nov 22, 2017 at 1:17
  • Does this answer your question: stackoverflow.com/questions/7353054/… Commented Nov 22, 2017 at 1:17

1 Answer 1

1

Here's how to do it with the subprocess module:

import subprocess
import sys

p1 = subprocess.Popen([sys.executable, "loop3.py"], stdout=subprocess.PIPE)
output, err = p1.communicate()

with open('opt.txt', 'w') as file:
    file.write(output.decode())
Sign up to request clarification or add additional context in comments.

Comments

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.