$? in linux gives the exit status of the recently executed command , as i have learnt.
suppose i give a cd command at the terminal and if such a directory or file is not present, echo $? following it gives me 1.

but, when the same is run via a script using os.system("cd.........."),
os.system("echo $?") gives 0.

it might be simple, but I am not able to trace out the reason.. can anyone throw some light on this?

6 Years
Discussion Span
Last Post by novice20

$? is a shell variable. Each time you call os.system, python creates a new shell. Anyway, os.system() is deprecated, use subprocess.Popen

import subprocess as sp
process = sp.Popen("cd /foo; echo $?", shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
print process.communicate()

""" my output -->
('1\n', '/bin/sh: line 0: cd: /foo: Aucun fichier ou dossier de ce type\n')
# (my shell speaks french :) )

I also wrote a small Command class which does about the same. See http://www.daniweb.com/code/snippet257449.html

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.