[Tutor] example of spawn

Martin Walsh mwalsh at groktech.org
Wed Jun 6 03:58:07 CEST 2007


Jason Coggins wrote:
> In the example listed below, reproduced from the web page you recommended, 
> what does `cp`, `cp` and `/dev/null` do?  I am assuming `index.html` is the 
> name of the program that will be launched.
> 
> example:----------------------------------------------
> os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
> --------------------------------------------------------

I don't fully understand the workings of os.spawnlp myself, but it seems
clear that the author of the example was writing it for use in a
unix/linux environment. 'cp' is a program (for copying files and
folders). And the second 'cp', 'index.html' & '/dev/null' -- the
argument list. So, the example above would seem somewhat equivalent to
the following linux shell command:

  host:~$ cp index.html /dev/null

which would copy the file 'index.html' (from the current directory) to
'/dev/null'.

'/dev/null' is a special file in a *nix environment that discards any
data written to it: http://en.wikipedia.org/wiki//dev/null
I can't think of a windows parallel to /dev/null.

I may be completely wrong, but I don't beleive os.spawnlp is available
on windows. But if you are a windows user, don't despair -- the
subprocess module equivalent should work in it's absence:
http://docs.python.org/lib/node538.html

import subprocess
# replacement for os.spawnlp(os.P_WAIT, ...)
retcode = subprocess.call(['notepad.exe', 'myfile.txt'])

In general, I believe use of the subprocess module is recommended over
os.spawn*, os.popen*, etc. -- regardless of the platform.

HTH,
Marty


More information about the Tutor mailing list