ok someone please explain popen to me it runs subproccesses does this mean it can run programs from within my program? any help would be appreciated , there is not an easily spottable place that explains this. Thanks

Dani AI

Generated

Short answer: yes — Popen/subprocess starts an external program (a child process) from inside Python and can connect the parent to the child's stdin, stdout and stderr so the parent can feed input, capture output, and check the exit status.

As suggested, prefer the subprocess module. For simple run-and-capture tasks the higher-level helpers are easiest; for more control use subprocess.Popen, which returns an object with methods like .communicate(), .wait(), .poll(), .terminate() and attributes such as .pid and .returncode. ’s calendar example illustrates capturing stdout from a child process; the patterns below show safer, more modern usage.

Example (safe pattern that avoids shell parsing):

import subprocess

result = subprocess.run(
    ['python', 'script.py'],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    text=True
)

print(result.returncode)
print(result.stdout)

Practical cautions and troubleshooting:

  • Prefer passing the command as a list (avoid shell=True) unless shell features are required; shell=True can allow command injection if input isn’t trusted.
  • Use .communicate() (or subprocess.run) instead of manual reads to avoid deadlocks when stdout/stderr are piped.
  • Check result.returncode (or use check=True) and inspect stderr for error messages.
  • For long-running or streaming programs, use Popen with non-blocking I/O or a separate thread/process to read output.
  • If a command is “not found”, confirm the executable path and PATH environment or use an absolute path.

For authoritative details and options (timeout, env, cwd, process groups, etc.), see the official Python subprocess documentation: subprocess module documentation.

Recommended Answers

All 6 Replies

Usually now a days recommended way is normally subprocess, there is also nice code snippet from gribouillis easying the use: A Command class to run shell commands.

These two code examples will illustrate the purpose and meaning of Popen ...

# save this code as calendar_may_2011.py 

import calendar

calendar.prmonth(2011, 5)

... now run this code ...

# calendar_may_2011_popen.py
# Popen (Pipe open) will open the 'pipe' containing the 
# standard output stream from a given Python program

import subprocess

# this will run the Python program in the working directory
# (or give full path of .py file so python.exe can find it)
cmd = "C:/Python27/python.exe calendar_may_2011.py"
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

# allow the program to finish
p.wait()

# pipe the standard output to string s
s = p.stdout.read()

print(s)

'''you should see -->

      May 2011
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

'''

The old os.popen() has been deprecated since Python version 2.6

I dont get exactly what it does still is there no good documentation for it? and how come you post on all my posts do you live on daniweb and post on all newb stuff? lol im no complaining you have helped me out a bit , are you a forum shark?

so it opens the program through the internals of the computer?

ok I think I get popen a little better.

I dont get exactly what it does still is there no good documentation for it? and how come you post on all my posts do you live on daniweb and post on all newb stuff? lol im no complaining you have helped me out a bit , are you a forum shark?

Those two are probably the most knowledgeable people on python here ;D

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.