473,416 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Python Doc Problem Example: os.system

Python Doc Problem Example: os.system

Xah Lee, 2005-09

today i'm trying to use Python to call shell commands. e.g. in Perl
something like

output=qx(ls)

in Python i quickly located the the function due to its
well-named-ness:

import os
os.system("ls")
however, according to the doc
http://www.python.org/doc/2.4/lib/os-process.html the os.system()
returns some esoteric unix thing, not the command output. The doc
doesn't say how to get the output of the command.

by chance someone told me that in python 2.4 the os.system is
supplanted by subprocess.call(), but this isn't mentioned in the doc!

upon finding the new doc location
http://www.python.org/doc/2.4/lib/mo...ubprocess.html i'm told that
this module replaces:

os.system
os.spawn*
os.popen*
popen2.*
commands.*
interesting. Since i'm not Python expert, i like to look at these. But
fuck, the incompetent doc gives ample gratis links to OpenSource this
or that or author masturbation links to remote book i don't really care
about, but here there's no link.

Problem summary:

* does not focus on the task users need to do. Instead, the doc is
oriented towards tech geeking.

* does not inform the reader at the right place where a new function is
replacing the old.

* does not provide relevant cross-links. (while provding many
irrelevant links because of OpenSource or Tech Geeking fanaticism)

Solution Suggestion:

* Add examples.

* Add cross-links to relevant modules.

* Mention and add link at the right place supplanted functions.

* Orient the doc to tasks and manifest functionalities. Think like
functional programing: input and output specification, and document
them. This will help focus and precision in the doc. Avoid prose-like
descriptions. Avoid drilling on remotely related tech/unix/C esoterica.
e.g. Do not mention as a documentation how they are implemented.
Mention implementation on the side if necessary. This way, the language
becomes focused as a independent tool (e.g. Mathematica, Java, Scheme,
emacs) (which may provide ample capabilities to interface/connect to
other technologies), instead of heavily intermixed and dependent with a
bunch of other things (unix things: Perl, Apache, shells).

-----------------------------
This article is archive at:
http://xahlee.org/UnixResource_dir/w...on_doc_os.html

Xah
xa*@xahlee.org
∑ http://xahlee.org/

Sep 5 '05 #1
2 4517
Xah Lee wrote:
Python Doc Problem Example: os.system

Xah Lee, 2005-09

today i'm trying to use Python to call shell commands. e.g. in Perl
something like

output=qx(ls)

in Python i quickly located the the function due to its
well-named-ness:

import os
os.system("ls")
however, according to the doc
http://www.python.org/doc/2.4/lib/os-process.html the os.system()
returns some esoteric unix thing, not the command output.
"""

*system*( command)

Execute the command (a string) in a subshell. This is implemented by
calling the Standard C function system(), and has the same
limitations. Changes to |posix.environ|, |sys.stdin|, etc. are not
reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded
in the format specified for wait(). Note that POSIX does not specify
the meaning of the return value of the C system() function, so the
return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell
after running command, given by the Windows environment variable
COMSPEC: on *command.com* systems (Windows 95, 98 and ME) this is
always |0|; on *cmd.exe* systems (Windows NT, 2000 and XP) this is
the exit status of the command run; on systems using a non-native
shell, consult your shell documentation.

Availability: Unix, Windows.

"""

Yup. Nothing more esoteric than a process's exit status. That's one of
those really tricky jargons that computer scientist idiots like to throw
around. You've got to watch out for those.
The doc
doesn't say how to get the output of the command.

by chance someone told me that in python 2.4 the os.system is
supplanted by subprocess.call(), but this isn't mentioned in the doc!

I'm presuming you mean in the os.system docs as you mention below that
you found such documentation.
upon finding the new doc location
http://www.python.org/doc/2.4/lib/mo...ubprocess.html i'm told that
this module replaces:

os.system
os.spawn*
os.popen*
popen2.*
commands.*
interesting.
"""
6.8 subprocess -- Subprocess management

New in version 2.4.

The subprocess module allows you to spawn new processes, connect to
their input/output/error pipes, and obtain their return codes. This
module intends to replace several other, older modules and functions,
such as:

os.system
os.spawn*
os.popen*
popen2.*
commands.*

"""
Yeah. There's a really tricky word up there in the beginning of the
subprocess doc. "intends". In this context, it means that it is
currently the plan of the Python developers to replace said modules with
the subprocess module, *however*, that has not totally come about now.
If the doc had said, "This module *has replaced* several others", then I
would have to agree with you that the stated module docs should be
updated to reflect the fact that they have been deprecated.
Since i'm not Python expert
Really?
, i like to look at these. But
fuck, the incompetent doc gives ample gratis links to OpenSource this
or that or author masturbation
OK - I just scanned through the subprocess module docs and I really
don't see where you're getting this from. I'll just chalk the former up
to your bad experience with the regular expression module docs referring
to the book "Mastering Regular Expressions." And since you're quite the
linguistic scholar, I'll chalk up the latter to your unique construction
of the book title I just cited.
links to remote book i don't really care
about, but here there's no link.

Problem summary:

* does not focus on the task users need to do. Instead, the doc is
oriented towards tech geeking.

Are you talking about the subprocess docs? If so, I'd like to see an
example of what you're talking about. Subprocess docs seem really
straightforward, terse, and to the point.
* does not inform the reader at the right place where a new function is
replacing the old.

I would leave it in the hands of the Python doc maintainers what to do
with this since subprocess hasn't yet totally replaced the other modules.
* does not provide relevant cross-links. (while provding many
irrelevant links because of OpenSource or Tech Geeking fanaticism)

I'd really like to see what you're talking about here. I just went
through the subprocess docs *again* and I don't see *any* links to any
other open source anything and I don't see any "tech geeking" to use
your jargon. I think you're full of crap. And I think you don't have
the balls to reply back to this message and show me what you're talking
about. You're just a little boy inside, making a call to a bowling
alley and asking if they have 15 pound balls and hanging up laughing
after they reply "yes" and you reply "Then how do you walk!!!" Oh,
you're so witty.
Solution Suggestion:

* Add examples.

Yeah, the

"""
6.8.3.2 Replacing shell pipe line

output=`dmesg | grep hda`
==>
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout)
output = p2.communicate()[0]
"""

subprocess module

"""
6.8.3.3 Replacing os.system()

sts = os.system("mycmd" + " myarg")
==>
p = Popen("mycmd" + " myarg", shell=True)
sts = os.waitpid(p.pid, 0)
"""

just

"""
6.8.3.4 Replacing os.spawn*

P_NOWAIT example:

pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid

P_WAIT example:

retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
==>
retcode = call(["/bin/mycmd", "myarg"])

Vector example:

os.spawnvp(os.P_NOWAIT, path, args)
==>
Popen([path] + args[1:])

Environment example:

os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
==>
Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
"""

doesn't have
"""
6.8.3.5 Replacing os.popen*

pipe = os.popen(cmd, mode='r', bufsize)
==>
pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout

pipe = os.popen(cmd, mode='w', bufsize)
==>
pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin

(child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdin, child_stdout) = (p.stdin, p.stdout)

(child_stdin,
child_stdout,
child_stderr) = os.popen3(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
(child_stdin,
child_stdout,
child_stderr) = (p.stdin, p.stdout, p.stderr)

(child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)

"""

any examples

"""
6.8.3.6 Replacing popen2.*

*Note:* If the cmd argument to popen2 functions is a string, the command
is executed through /bin/sh. If it is a list, the command is directly
executed.

(child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
==>
p = Popen(["somestring"], shell=True, bufsize=bufsize
stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdout, child_stdin) = (p.stdout, p.stdin)

(child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode)
==>
p = Popen(["mycmd", "myarg"], bufsize=bufsize,
stdin=PIPE, stdout=PIPE, close_fds=True)

(child_stdout, child_stdin) = (p.stdout, p.stdin)

"""

at all.
* Add cross-links to relevant modules.

Bah. Whatever. I've wasted enough time on this thread. I think the
only reason I read your posts is because you're just so comically and
consistently off base.
* Mention and add link at the right place supplanted functions.

* Orient the doc to tasks and manifest functionalities. Think like
functional programing: input and output specification, and document
them. This will help focus and precision in the doc. Avoid prose-like
descriptions. Avoid drilling on remotely related tech/unix/C esoterica.
e.g. Do not mention as a documentation how they are implemented.
Mention implementation on the side if necessary. This way, the language
becomes focused as a independent tool (e.g. Mathematica, Java, Scheme,
emacs) (which may provide ample capabilities to interface/connect to
other technologies), instead of heavily intermixed and dependent with a
bunch of other things (unix things: Perl, Apache, shells).

-----------------------------
This article is archive at:
http://xahlee.org/UnixResource_dir/w...on_doc_os.html

Xah
xa*@xahlee.org
∑ http://xahlee.org/

JMJ
Sep 5 '05 #2
Xah Lee schrieb:
Python Doc Problem Example: os.system

Xah Lee, 2005-09

today i'm trying to use Python to call shell commands. e.g. in Perl
something like

output=qx(ls)

in Python i quickly located the the function due to its
well-named-ness:

import os
os.system("ls")
however, according to the doc
http://www.python.org/doc/2.4/lib/os-process.html the os.system()
returns some esoteric unix thing, not the command output. The doc
doesn't say how to get the output of the command.


The os.popen(...) function will return a file like object, so you can
use a read() - method of these object to get the called programms output.

hope that helps - I' m very new to python

The documentation may be not well for all circumstances but the fine
thing is: the documentation is part of the code itself - inform of the
docstrings - and this is a very good approach. The bad thing is the
person who wrote the docstring was documenting his/her own code and not
code of other programmers.
Sep 5 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: Ixokai | last post by:
Hello all. :) I've been a long time Python fan, and have fairly recently (with the support of a coworker who surprised me by mentioning my pet language one day) convinced my company to begin the...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
122
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.