Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5240 > unrolled thread

Re: os.popen command working differently on Windows

Started byTim Golden <mail@timgolden.me.uk>
First post2011-05-12 15:21 +0100
Last post2011-05-13 19:10 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: os.popen command working differently on Windows Tim Golden <mail@timgolden.me.uk> - 2011-05-12 15:21 +0100
    Re: os.popen command working differently on Windows Nobody <nobody@nowhere.com> - 2011-05-13 09:48 +0100
    Re: os.popen command working differently on Windows Hans Mulder <hansmu@xs4all.nl> - 2011-05-13 19:10 +0200

#5240 — Re: os.popen command working differently on Windows

FromTim Golden <mail@timgolden.me.uk>
Date2011-05-12 15:21 +0100
SubjectRe: os.popen command working differently on Windows
Message-ID<mailman.1469.1305210153.9059.python-list@python.org>
On 12/05/2011 15:11, Ayaskanta Swain wrote:
> Please help me in solving the following issue I am facing while
> executing my python script. Basically I am executing the OS specific
> move command to move a file/dir from one location to another.

Why? Why not use os.rename or shutil.move which already do
whatever is needed under the covers for different Operating Systems?

os.popen returns a file-like object from which you can read any
error messages generated. You're not doing that, and os.popen
won't raise an error itself unless you, say, pass it a number
rather than a string.

<code>
import os

output = os.popen ("dir")
print output.read ()

#
# But note:
#
os.popen ("Nonsen*se")

# raises no exception

</code>

TJG

[toc] | [next] | [standalone]


#5286

FromNobody <nobody@nowhere.com>
Date2011-05-13 09:48 +0100
Message-ID<pan.2011.05.13.08.48.23.250000@nowhere.com>
In reply to#5240
On Thu, 12 May 2011 15:21:41 +0100, Tim Golden wrote:

> os.popen returns a file-like object from which you can read any error
> messages generated.

The documentation doesn't say, but if it's anything like the Unix popen()
function, with mode='r' the returned file-like object will correspond to
the child's stdout, but error messages will normally be written to stderr.

In any case, os.popen() and similar are deprecated in favour of
subprocess.Popen().

[toc] | [prev] | [next] | [standalone]


#5309

FromHans Mulder <hansmu@xs4all.nl>
Date2011-05-13 19:10 +0200
Message-ID<4dcd66d6$0$81483$e4fe514c@news.xs4all.nl>
In reply to#5240
On 12/05/2011 16:21, Tim Golden wrote:
> On 12/05/2011 15:11, Ayaskanta Swain wrote:
>> Please help me in solving the following issue I am facing while
>> executing my python script. Basically I am executing the OS specific
>> move command to move a file/dir from one location to another.
>
> Why? Why not use os.rename or shutil.move which already do
> whatever is needed under the covers for different Operating Systems?
>
> os.popen returns a file-like object from which you can read any
> error messages generated. You're not doing that, and os.popen
> won't raise an error itself unless you, say, pass it a number
> rather than a string.
>
> <code>
> import os
>
> output = os.popen ("dir")
> print output.read ()
>
> #
> # But note:
> #
> os.popen ("Nonsen*se")
>
> # raises no exception
>
> </code>

If you want exceptions, try the subprocess module:

 >>> import subprocess
 >>> subprocess.Popen("Nonsen*se")
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", 
line 672, in __init__
     errread, errwrite)
   File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", 
line 1201, in _execute_child
     raise child_exception
OSError: [Errno 2] No such file or directory
 >>>

In fact, os.popen is deprecated in favour of subprocess.Popen().

HTH,

-- HansM

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web